express-restful-helper

0.0.3 • Public • Published

express-restful-helper

NPM version Build Status

Express middleware to attach some common responses to the response object as helper-methods. This helper tries to enforce the best practices from Thinking Mobile/Blog.

Installation

$ npm install --save express-restful-helper

Usage

The express-restful-helper middleware have to be attached before the routes are created. It will attach methods to the response object from express. function(req, res, next).

var express = require('express');
var app = express();
var expressRestfulHelper = require('express-restful-helper');

let options = {
    showInfo: true // Show helpful generic information about the response code.
    showHttp: true // Show the http status code as text (e.g. UNATHORIZED ACCESS)
    showStatus: true // Show the status code in the response
}
app.use(expressRestfulHelper(options));

app.get('/', (req, res, next) => {
    res.ok({
        message: 'successful request.'
    });
});

Attached Methods

The below methods are attached to the response object. The basic express response is still available.

    res.ok(data);                  // 200, OK: Request was successful.
    res.okNewResource(data);       // 201, CREATED: Request successfully created new resource.
    res.okNoContent(data);         // 204, OK NO CONTENT: Request was successfull and no content returned.
    res.badRequest(data);          // 400, BAD REQUEST: Bad Request (invalid input)
    res.unauthorized(data);        // 401, UNAUTHORIZED ACCESS: Unathorized Access
    res.forbidden(data);           // 403, FORBIDDEN: Forbidden access
    res.notFound(data);            // 404, NOT FOUND: Not found
    res.unprocessableEntity(data); // 422, UNPROCESSABLE ENTITY: Data supplied not processable.
    res.internalServerError(data); // 500, INTERNAL SERVER ERROR: Error occured on server.
    res.api(statusCode, data, infoMessage, httpMessage); // Custom message

Respons Format

The options have three fields, showInfo, showHttp, showStatus that affects the response format (see below).

{
    "http": "message",          // If option.showHttp = true
    "info": "message",          // If option.showInfo = true
    "status": "[status code]"   // If option.showStatus = true (this is ALWAYS available in the HEADERS)
    "data" : "[supplied data/errors]" // The payload when calling the attached method.
}

Options

The options are used when using express.use() as a function parameter.

let options = {
    showInfo: true // Show helpful generic information about the response code.
    showHttp: true // Show the http status code as text (e.g. UNATHORIZED ACCESS)
    showStatus: true // Show the status code in the response
}
app.use(expressRestfulHelper(options));

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

License

MIT © Tommy Dronkers

Package Sidebar

Install

npm i express-restful-helper

Weekly Downloads

2

Version

0.0.3

License

MIT

Last publish

Collaborators

  • steeljuice