JSPM

  • Created
  • Published
  • Downloads 51
  • Score
    100M100P100Q74644F
  • License MIT

Configurable node rest server

Package Exports

  • node-rest-server

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-rest-server) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

node-rest-server

NPM

Configuration only node rest server

The library will start the express server by just using routes configuration.

Features

  • Ready to use rest server in minutes.
  • Free from all boilerplate code for creating and managing the server, so that developer can focus on actual business logic.
  • Simple configuration to generate response data.

Where you can use

  • Can be used as a stub server for any application(like ReactJS, AngularJS) to mock server response during development.

  • Can be used for creating rest micro-service in minutes (help me improve this library)

Installation

This is a Node.js module available through the npm registry. Install using below command.

npm install --save node-rest-server

Importing

import RestServer from "node-rest-server"; // ES6
or
var RestServer = require("node-rest-server"); // ES5

// call it as function and pass configuration
RestServer(routeConfig, serverConfig);

Usage Example

import RestServer from "node-rest-server";

const routeConfig = {
    '/api1': {
        method: 'GET',
        status: 200,
        controller: () => 'Data',
    },
};

RestServer(routeConfig);

Sample

example directory provides a sample application explaining the use of this library.

Route Configuration

A route configuration is an object with key(route path) value(route options) pair:-

  1. Path: Uri which will serve a resource in rest server
  2. Route Options: Options which define working of the path and also decide status and response payload.

Route Options

Name Type Default Description
method {string} GET Method defines the type of request controller will handle
controller {function|object} This function/object will contain the business logic for the route path. For a function an object is passed which will contain request url, body, params and header to be used.
status (optional) {string} 200 An appropriate HTTP response status code which server will give response for a request

Controller method

A controller can either

  • return an object with status and payload;
{
  status: 500, // should be a number
  payload: "Hello world" // user can send any valid json converted using JSON.stringify()
}

or

  • return a response data object (valid as per JSON.stringify() json spec)

Example

const routeConfig = {
  '/endpoint1': {
    method: 'GET',
    status: 200,
    controller: () => 'Data',
  },
  '/endpoint2': {
    method: 'POST',
    controller: requestData => {
      return { status: 200, payload: { data: 'Data' } };
    },
  },
}

Server Configuration (optional)

This manages how the server will be configured

Name Type Default Description
basePath {string} Common prefix for all the routes
port {Number} 8000 Port on which server will serve the content
delay (sec) {Number} 0 Forcefully delay the response timing in seconds
logger {Object|Boolean} true Enable logging for application, a boolean value will enable/disable all logging features, an object can be passed with property enable to toggle the logging and debug to enable/disable debug logs
filter {Function} Enable application level filter and pass returned value to controller.

Example

const serverConfig = {
  basePath: '/base/api',
  port: 8080,
  delay: 2,
  logger: {
      enable: true,
      debug: false,
  },
  filter: (requestData) => {
      return { data: 'calculate' };
  },
};