Everything you need to start creating NodeJS based API Servers.
$ npm i --save @jordanforeman/api-framework
import { setup, start } from '@jordanforeman/api-framework';
import controllers from './controllers';
const config = {
port: 8080
};
setup(controllers, config);
start();
- as of
v3.0.0
,setup
andstart
are two separate lifecycle methods. This enables you to define any special logic that is required to take place between configuration (ie.server.use
) and start (ie.server.listen
).
Option | Description | Default |
---|---|---|
port |
The port to run the server on | 8080 |
onStart |
(optional) a function to run when the server starts | n/a |
plugins |
(optional) an array of express plugins to include (eg. express-session ) |
n/a |
onError |
(optional) a function to run when the server encounters an error (takes a single parameter, which is the error) | n/a |
This module is an opinionated wrapper around the Express framework, however controller syntax is heavily inspired by HapiJS. The controllers
parameter passed to the start
method is an array of objects with the following structure:
Property | Description |
---|---|
path | The API route/path |
method | the standard HTTP method to use (maps to Express router methods). One of HTTPMethod |
config.handler | A single Express method callback with signature (request, response, next) |
config.middleware | (optional) the middleware (or middlewares) to use for this controller |
config.auth | (optional) the authentication strategy to use for this controller (see below) |
import { auth as jwtAuth } from '@jordanforeman/jwt-authentication';
export const myController = {
path: '/',
method: 'GET',
config: {
auth: jwtAuth,
handler: getRoot
}
};
Authentication strategies are published separately from this module. The following strategies are implemented to work with this module: