1.2.0 • Published 6 years ago

express-rest-service v1.2.0

Weekly downloads
7
License
MIT
Repository
github
Last release
6 years ago

express.rest.service

Create REST service for publishing with express

Installation

npm install express-rest-service

Usage

const ExpressRESTService = require("express-rest-service");

require express-rest-service

const myRESTService = new ExpressRESTService(props);

create REST service instance with properties

const app = express();

create express app

app.get('/foo', (req, res) => myRESTService.call(req, res));

register service in express app

Properties

  • args object with keys that represent request argunents to service
  • fn function that will be called on service call with arguments (service, request, response)
  • hasAccess function that will be called with arguments (service, request, callback) to check is user logged in or has user access to this service or not. function should call callback with 2 boolean arguments (loggedIn, hasAccess)
  • prettyJSON boolean false by default

Example

const myRESTService = new ExpressRESTService({
  args: {
    firstName: false,
    lastName: true,
    age: "number",
    height: {
      type: "number",
      mandatory: true,
      decimalPrecision: 2
    }
  },
  fn: (service, request, response) => {
    service.success({
    data: service.args
    });
  }
});

app.get('/foo', (req, res) => myRESTService.call(req, res));