Skip to content

danielemoraschi/express-http-response

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

express-http-response

NPM Version NPM Downloads Build Status Test Coverage MIT License

Node.js http response middleware to provide a standard response output.

Install

npm install --save http-response-middleware

Example

// # app.js
var express = require('express');
var httpResponse = require('express-http-response');

var BadRequestResponse = httpResponse.BadRequestResponse;
var OkResponse = httpResponse.OkResponse;
var app = express();

app.get('/users', function(req, res, next) {

    if (req.missing_parameter) {
        throw new BadRequestResponse('Missing required parameter.');
        /**
        which will return:
        Header status code: 400
        {
            type: "BadRequestResponse",
            message: "Missing required parameter.",
            status: 400,
            success: false
        }
        logging to console.error
        */
    }

    next(new OkResponse({
        users: { /* ... */ },
        page: 1,
        total: 42
    }));

    /**
    which will return:
    Header status code: 200
    {
        type: "OkResponse",
        message: "OK",
        status: 200,
        success: true,
        data: {
            users: { },
            page: 1,
            total: 42
        }
    }
    */

});

// keep this after all routes that will use the response object
app.use(httpResponse.Middleware);

app.listen(3000);

API

Middleware

The middleware will parse the thrown response returning to the user with a standard json response.

var httpResponse = require('express-http-response');
var express = require('express');
var app = express();

/** [app routes] */

app.use(httpResponse.Middleware);

HttpResponse

var HttpResponse = require('express-http-response').HttpResponse;

var response = new HttpResponse (data, message, httpStatusCode, errorCode, moreInfo, success);

/*
response == {
    type: "HttpResponse",     // String
    data: `data`,             // Mixed/Object | not present if undefined
    message: `message`,       // String
    status: `httpStatusCode`, // Int
    code: `errorCode`,        // Mixed/Object | not present if undefined
    moreInfo: `moreInfo`,     // Mixed/Object | not present if undefined
    success: `success`        // Boolean, false by default
}
*/

OkResponse

var OkResponse = require('express-http-response').OkResponse;

var response = new OkResponse (data, message, moreInfo);

/*
response == {
    type: "OkResponse",       // String
    data: `data`,             // Mixed/Object | not present if undefined
    message: `message`,       // String
    status: 200,              // Int
    moreInfo: `moreInfo`,     // Mixed/Object | not present if undefined
    success: true             // Boolean
}
*/

BadRequestResponse

var BadRequestResponse = require('express-http-response').BadRequestResponse;

var response = new BadRequestResponse (message, errorCode, moreInfo);

/*
response == {
    type: "BadRequestResponse", // String
    message: `message`,       // String
    status: 400,              // Int
    moreInfo: `moreInfo`,     // Mixed/Object | not present if undefined
    success: false            // Boolean
}
*/

UnauthorizedResponse

var UnauthorizedResponse = require('express-http-response').UnauthorizedResponse;

var response = new UnauthorizedResponse (message, errorCode, moreInfo);

/*
response == {
    type: "UnauthorizedResponse", // String
    message: `message`,       // String
    status: 401,              // Int
    moreInfo: `moreInfo`,     // Mixed/Object | not present if undefined
    success: false            // Boolean
}
*/

ForbiddenResponse

var ForbiddenResponse = require('express-http-response').ForbiddenResponse;

var response = new ForbiddenResponse (message, errorCode, moreInfo);

/*
response == {
    type: "ForbiddenResponse", // String
    message: `message`,       // String
    status: 403,              // Int
    moreInfo: `moreInfo`,     // Mixed/Object | not present if undefined
    success: false            // Boolean
}
*/

NotFoundResponse

var NotFoundResponse = require('express-http-response').NotFoundResponse;

var response = new NotFoundResponse (message, errorCode, moreInfo);

/*
response == {
    type: "NotFoundResponse", // String
    message: `message`,       // String
    status: 404,              // Int
    moreInfo: `moreInfo`,     // Mixed/Object | not present if undefined
    success: false            // Boolean
}
*/

MethodNotAllowedResponse

var MethodNotAllowedResponse = require('express-http-response').MethodNotAllowedResponse;

var response = new MethodNotAllowedResponse (message, errorCode, moreInfo);

/*
response == {
    type: "MethodNotAllowedResponse", // String
    message: `message`,       // String
    status: 405,              // Int
    moreInfo: `moreInfo`,     // Mixed/Object | not present if undefined
    success: false            // Boolean
}
*/

ConflictResponse

var ConflictResponse = require('express-http-response').ConflictResponse;

var response = new ConflictResponse (message, errorCode, moreInfo);

/*
response == {
    type: "ConflictResponse", // String
    message: `message`,       // String
    status: 409,              // Int
    moreInfo: `moreInfo`,     // Mixed/Object | not present if undefined
    success: false            // Boolean
}
*/

InternalServerErrorResponse

var InternalServerErrorResponse = require('express-http-response').InternalServerErrorResponse;

var response = new InternalServerErrorResponse (message, errorCode, moreInfo);

/*
response == {
    type: "InternalServerErrorResponse", // String
    message: `message`,       // String
    status: 500,              // Int
    moreInfo: `moreInfo`,     // Mixed/Object | not present if undefined
    success: false            // Boolean
}
*/

License

MIT

About

Node.js http response middleware to provide a standard response output.

Resources

License

Stars

Watchers

Forks

Packages

No packages published