Application events
Application events lets you hook your serverless functions to external events.
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 install codehooks-js --save
API quick overview
- Complete code example
- app.init()
- app.use(workerFunction)
- app.use(route, workerFunction)
- app.get(route, workerFunction)
- app.post(route, workerFunction)
- app.put(route, workerFunction)
- app.patch(route, workerFunction)
- app.delete(route, workerFunction)
- app.all(route, workerFunction)
- app.auth(route, workerFunction)
- app.worker(name, workerFunction)
- app.job(cronExpression, workerFunction)
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
// 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
- workerFunction: function(requestObject, responseObject, next)
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
- route: string that matches route
- workerFunction: function(requestObject, responseObject, next)
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
- route: string that matches route
- workerFunction: function(requestObject, responseObject)
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
- route: string that matches route
- workerFunction: function(requestObject, responseObject)
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
- route: string that matches route
- workerFunction: function(requestObject, responseObject)
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
- route: string that matches route
- workerFunction: function(requestObject, responseObject)
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
- route: string that matches route
- workerFunction: function(requestObject, responseObject)
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
- route: string that matches route
- workerFunction: function(requestObject, responseObject)
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
- route: string that matches route
- workerFunction: function(requestObject, responseObject)
Returns void
Code example
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
Code example for scheduled workers
app.job(cronExpression, workerFunction)
Execute workerFunction function when cron job are scheduled.
Parameters
- cronExpression: a valid cron expression string
- workerFunction: function(jobData, responseFunction)
Returns void
Code example