5.2.0 • Published 2 years ago

@jordanforeman/api-framework v5.2.0

Weekly downloads
9
License
MIT
Repository
github
Last release
2 years ago

@jordanforeman/api-framework

Everything you need to start creating NodeJS based API Servers.

semantic-release npm.io npm.io Renovate enabled

Installation

$ npm i --save @jordanforeman/api-framework

Usage

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 and start 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).

Configuration Options

OptionDescriptionDefault
portThe port to run the server on8080
onStart(optional) a function to run when the server startsn/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

Controller Syntax

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:

PropertyDescription
pathThe API route/path
methodthe standard HTTP method to use (maps to Express router methods). One of HTTPMethod
config.handlerA 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

Authentication strategies are published separately from this module. The following strategies are implemented to work with this module:

Helpful Links

5.2.0

2 years ago

5.1.0

2 years ago

5.0.0

2 years ago

4.0.0

2 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago