node package manager
Don’t reinvent the wheel. Reuse code within your team. Create a free org »

json-rest-serve

JSON REST Serve

A NodeJs tool to serve a JSON data as REST end points.

Install

$ npm install json-rest-serve

Requirements

  • The JSON data should be an array of objects.
  • Each object (data item) should be identifiable with a primary key property.
  • The primary key should be either string or integer.
  • The data item can have nested objects within it.
  • The added resource should have atleast one data item in its data array.
  • The tool uses the first data item to understand the structure of it. It then uses this information to enforce the same structure during the POST and PUT operations.

How to use

Steps:

  • Add one or more resources that you want to serve as REST endpoints.
  • After adding, call the serve method with your preferred port number.
  • name, key and data are required to add a resource.
'use strict';
 
let jsonRestServe = require('json-rest-serve');
 
jsonRestServe.resource.add({
    root: 'api',
    name: 'employee',
    key: 'id',
    data: require('./data/employee.json'),
    pageSize: 8
});
 
jsonRestServe.resource.add({
    root: 'static',
    name: 'country',
    key: 'code',
    data: require('./data/country.json'),
    pageSize: 25,
    readOnly: true
});
 
jsonRestServe.serve(9000);

Outcome

Options and Parameters

resource.add(options)

Key Type Default Description
root String api Forms the base of rest urls after http://localhost:9000
name String Required field This would be the name of the resource
key String Required field Name of the primary key attribute of the data item.
version String This version value would be added to the url. Example: http://localhost:9000/v1/api/employee
data Array Required field An array of objects with a definite primary key
pageSize Integer 10 Used in get all operation which is paginated.
schema Object The tool generates a schema when not provided You can provide a robust schema for your JSON data item. The schema should adhere to draft-04 of json-schema.org

serve(portNumber, morganLogFormat)

Parameter Type Default Description
portNumber Integer 9000 Port number of the REST endpoints
morganLogFormat String dev Takes pre-defined formats provided by Morgan logging tool such as combined, common, dev, short, tiny, etc.

Features

  • Can serve any array of objects JSON data with any structure and with any primary key.
  • Can add as many resources before calling serve method.
  • Does not update the json file for PUT/POST/DELETE operations. All the changes you make through the REST calls will be lost once you terminate the process.
  • CORS enabled
  • Can be used as a developer tool and not for any production deployment.
  • Filter, sort, search and field selection features are available with GET requests. See the generated apidocs (http:/documents/index.html) for more information.

Driven by

  • Primarily powered by Lodash and Restify
  • Supported by other libraries such as Morgan, Ajv and Rimraf.
  • Document site is powered by apidoc.

License

MIT