Skip to content

Everything you need to start creating a NodeJS based API Server

Notifications You must be signed in to change notification settings

JordanForeman/api-framework

Repository files navigation

@jordanforeman/api-framework

Everything you need to start creating NodeJS based API Servers.

NPM version semantic-release 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

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

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:

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

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

Helpful Links

About

Everything you need to start creating a NodeJS based API Server

Resources

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •