1.2.2 • Published 7 years ago

json-rest-serve v1.2.2

Weekly downloads
-
License
MIT
Repository
bitbucket
Last release
7 years ago

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)

KeyTypeDefaultDescription
rootStringapiForms the base of rest urls after http://localhost:9000
nameStringRequired fieldThis would be the name of the resource
keyStringRequired fieldName of the primary key attribute of the data item.
versionStringThis version value would be added to the url. Example: http://localhost:9000/v1/api/employee
dataArrayRequired fieldAn array of objects with a definite primary key
pageSizeInteger10Used in get all operation which is paginated.
schemaObjectThe tool generates a schema when not providedYou can provide a robust schema for your JSON data item. The schema should adhere to draft-04 of json-schema.org

serve(portNumber, morganLogFormat)

ParameterTypeDefaultDescription
portNumberInteger9000Port number of the REST endpoints
morganLogFormatStringdevTakes 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

1.2.2

7 years ago

1.2.1

7 years ago

1.1.1

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago