codehooks-js
v1.2.20
Published
Codehooks.io official library - provides express.JS like syntax
Downloads
245
Maintainers
Readme
codehooks-js
Codehooks.io official development library - provides Express.JS-like syntax for using codehooks.io.
Read the Quickstart for developers.
For more details navigate to official docs: https://codehooks.io/docs.
Install
Install the Codehooks CLI:
$ npm install -g codehooks
Next install the codehooks-js NPM package in your project.
npm install codehooks-js
JavaScript ES6 development
Example code for a serverless backend API and NoSQL database:
/*
* REST API with NoSQL database storage.
* Codehooks (c) example code.
*/
import {app, datastore} from 'codehooks-js';
// Example GET route and a NoSQL database insert operation
app.get('/myroute', async (req, res) => {
console.log("GET")
const conn = await datastore.open()
const doc = await conn.insertOne('greetings', {"message": "Hello World!", "when": new Date()})
res.json({...doc})
});
// Serve web content from the uploaded directory /static
app.static({directory: "/static"})
// CRUD REST API for any collection
app.crudlify({}, {prefix: "/"})
// return app to serverless runtime engine
export default app.init()
TypeScript development
Rename the index.js to index.ts or create a new index.ts file. Start developing using TypeScript and strong types shown in the example code below.
/*
* REST API with NoSQL database storage.
* Codehooks (c) example code in TypeScript.
*/
import {app, datastore, httpResponse, httpRequest} from 'codehooks-js';
// Example GET route and a NoSQL database insert operation
app.get('/myroute', async (req: httpRequest, res: httpResponse) => {
console.log("GET")
const conn = await datastore.open()
const doc = await conn.insertOne('greetings', {"message": "Hello World!", "when": new Date()})
res.json({...doc})
});
// Serve web content from the uploaded directory /static
app.static({directory: "/static"})
// CRUD REST API for any collection
app.crudlify({}, {prefix: "/"})
// return app to serverless runtime engine
export default app.init()
Compile
When running the coho compile
command, it will automatically create a tsconfig.json
file in the project directory. The tsconfig file can be further adapted to your needs, the initial configuration is shown in the example below:
{
"files": [
"./index.ts"
],
"compilerOptions": {
"allowJs": true,
"lib": [
"ES6",
"dom"
]
}
}
Any syntax or type error will be displayed accordingly from the compile command, for example:
$ coho compile
🤔 [tsl] ERROR in /Users/jane/projects/tsdemo/index.ts(9,9)
TS2345: Argument of type '(req: httpRequest, res: httpResponse) => void' is not assignable to parameter of type 'string'.
Correcting the errors should ultimately show a successfull compile output, ready for deployment of your backend app.
$ coho compile
OK 🙌
Deploy
From the project directory run:
$ codehooks deploy