Skip to main content

The Application Object

The Application object creates events that lets you hook your serverless functions accordingly.

Available event types are:

  • REST API route events
  • Background Job cron and scheduled events
  • Worker Queue events
  • Authentication events

The event hooks is an Node.js Express style JavaScript library in a public npm library that you install as a npm package in your project directory. You can check for updates in the npm version.

npm i codehooks-js

API quick overview

Complete code example

Import the app library in your application and hook into the various events.

import {app} from 'codehooks-js'

// generic serverless function
function fooFunc(req, res) {
res.end()
}
// generic middleware function
function fooMiddleware(req, res, next) {
next()
}

/*
* Hook into events
*/
app.use(fooMiddleware); // global middleware
app.use('/foo', fooMiddleware); // global middleware on routes
app.get('/foo', fooFunc); // GET
app.post('/foo', fooFunc); // POST
app.put('/foo', fooFunc); // PUT
app.patch('/foo', fooFunc); // PATCH
app.delete('/foo', fooFunc); // DELETE
app.all('/foo', fooFunc); // GET, POST, PUT, PATCH, DELETE
app.auth('/*', fooMiddleware); // Called before outher routes without access tokens
app.job('* * * * *', fooFunc); // subscribe to cron events
app.worker('workername', fooFunc); // subscribe to worker events
app.static(options); // serve static files and content

// Bind events and functions to the serverless runtime
export default app.init();

app.init()

Mandatory call to bind your functions to events from the serverless runtime.

Parameters none

app.use(workerFunction)

Adds global middleware to all route API events (get, put, post, patch, delete).

Parameters

Returns void

Code example

import {app} from 'codehooks-js'
import cookieParser from 'cookie-parser'

// external npm lib middleware
app.use(cookieParser())

app.get('/foo', function (req, res) {
// Cookies that have not been signed
console.log('Cookies: ', req.cookies)

// Cookies that have been signed
console.log('Signed Cookies: ', req.signedCookies)

// Custom middleware result
console.log('Foo: ', req.foo)

res.send('Done')
})

export default app.init();

app.use(route, workerFunction)

Adds global middleware to a specific route API events (get, put, post, patch, delete).

Parameters

Returns void

Code example

import {app} from 'codehooks-js'

// custom
app.use('/foo', (req, res, next) => {
console.log('Called before any other route handlers')
req.foo = 'Foo was here!'
next() // must be called to continue
})

app.get('/foo', function (req, res) {
// Custom middleware result
console.log('Foo: ', req.foo)
res.send('Done')
})

export default app.init();

app.get(route, workerFunction)

Execute workerFunction function on HTTP GET method and route match.

Parameters

Returns void

Code example

import {app} from 'codehooks-js'

app.get('/foo', function (req, res) {
res.send('Done')
})

export default app.init();

app.post(route, workerFunction)

Execute workerFunction function on HTTP POST method and route match.

Parameters

Returns void

Code example

import {app} from 'codehooks-js'

app.post('/foo', function (req, res) {
console.log("Data from client is", req.body)
res.send('Done')
})

export default app.init();

app.put(route, workerFunction)

Execute workerFunction function on HTTP PUT method and route match.

Parameters

Returns void

Code example

import {app} from 'codehooks-js'

app.put('/foo', function (req, res) {
console.log("Data from client is", req.body)
res.send('Done')
})

export default app.init();

app.patch(route, workerFunction)

Execute workerFunction function on HTTP PATCH method and route match.

Parameters

Returns void

Code example

import {app} from 'codehooks-js'

app.patch('/foo', function (req, res) {
console.log("Data from client is", req.body)
res.send('Done')
})

export default app.init();

app.delete(route, workerFunction)

Execute workerFunction function on HTTP DELETE method and route match.

Parameters

Returns void

Code example

import {app} from 'codehooks-js'

app.delete('/foo/:ID', function (req, res) {
const {ID} = req.params;
console.log("Delete this", ID)
res.send('Done')
})

export default app.init();

app.all(route, workerFunction)

If no other routes matched before this, then execute the workerFunction function on all HTTP methods and route match.

Parameters

Returns void

Code example

import {app} from 'codehooks-js'

app.get('/foo', function (req, res) {
res.send('Done GET')
})

app.all('/*', function (req, res) {
res.send(`None of the above routes matched but this ${req.method} ${req.originalUrl}`)
})

export default app.init();

app.auth(route, workerFunction)

Execute workerFunction function when no authentication token (x-apikey or JWT) is present. Works for all HTTP methods and route matches.

Parameters

Returns void

Code example

See Auth hooks docs here.

app.worker(name, workerFunction)

Register a named workerFunction function that can be used to process queued events or scheduled events.

Parameters

  • name: unique worker name
  • workerFunction: function(queueData, responseFunction)

Returns void

Code example for queue workers

See Queue docs here.

Code example for scheduled workers

See scheduled jobs docs here.

app.job(cronExpression, workerFunction)

Execute workerFunction function when cron job are scheduled.

Parameters

  • cronExpression: a valid cron expression string
  • workerFunction: function(jobData, responseFunction)

See Job hook docs here.

app.static(options, callback)

Serve static files from deployed source directory.

For example: app.static({ route: '/img', directory: '/assets/images' })

Options

  • directory: path to file upload directory
  • route: URL sub route for clients

callback(req, res, next)

Provide a middleware function to control the request. For example, add client side cache headers instructions for static assets.

// serve static assets/images, and cache for 1 hour
app.static({ route: '/assets', directory: '/assets' }, (req, res, next) => {
console.log('If you see this, the client cache is invalidated or called for the first time');
const ONE_HOUR = 1000*60*60;
res.set('Cache-Control', `public, max-age=${ONE_HOUR}, s-maxage=${ONE_HOUR}`);
res.setHeader("Expires", new Date(Date.now() + ONE_HOUR).toUTCString());
res.removeHeader('Pragma');
next();
})

app.storage(options, callback)

Serve files from the blob storage file system. You can upload files to the blob storage with the CLI.

$ coho file-upload --projectname 'myproject-fafc' --space 'dev' --src '/mylocaldir/' --target '/mydocuments'

For example: app.storage({ route: '/docs', directory: '/mydocuments' })

Options

  • directory: path to file upload directory
  • route: URL sub route for clients

callback(req, res, next)

Provide a middleware function to control the request.