erh

express-restful-helper

Express middleware to attach some common responses to the response object as helper-methods.

Showing:

Popularity

Downloads/wk

4

Maintenance

No Maintenance Data Available

Package

Dependencies

1

Size (min+gzip)

0.9KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

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

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100