ejs

express-json-status

format the json response including HTTP status and description

Showing:

Popularity

Downloads/wk

2

GitHub Stars

1

Maintenance

Last Commit

7yrs ago

Contributors

0

Package

Dependencies

2

License

ISC

Type Definitions

Tree-Shakeable

No?

Categories

Readme

express-json-status

Format the json response including HTTP status and description

Installation

npm install --save json-response
var expressJsonStatus = require('express-json-status');
var HTTPStatus = expressJsonStatus.HTTPStatus;

##Usage

require('express-json-status');
var express = require('express'), app = express();

app.get('/', function (req, res) {
    var user = {name: 'john'};
    res.jsons(user);
});

app.get('/add', function (req, res) {
    var user = null;
    if(user==null)
        return res.jsons(500, {error: 'user null'});
    res.jsons({msg:'sucess'})
});

app.listen(3000);


output http://localhost:3000/:

{
    "status": "200",
    "description": "OK",
    "data": {
        "name": "john"
    }
}

output http://localhost:3000/add:

{
    "status": "500",
    "description": "Internal Server Error",
    "data": {
        "error": "user null"
    }
}

##Custom output json

var expressJsonStatus = require('express-json-status');
var HTTPStatus = expressJsonStatus.HTTPStatus;
var express = require('express'), app = express();


expressJsonStatus.format = function(statusCode, description, data){
    var a = {};
    a[statusCode] = description;
    a.data = data;
    return a;
};

app.get('/addformat', function (req, res) {
    var user = null;
    if(user==null)
        return res.jsons(HTTPStatus.INTERNAL_SERVER_ERROR, {error: 'user null'});
    res.json({msg:'sucess'})
});

app.listen(3000);

output http://localhost:3000/addformat:

{
    "500": "Internal Server Error",
    "data": {
        "error": "user null"
    }
}

##HTTPStatus

Utility to interact with HTTP status code.

var status = HTTPStatus.INTERNAL_SERVER_ERROR; //500

200: OK 
400: BAD_REQUEST
401: UNAUTHORIZED
404: NOT_FOUND
500: INTERNAL_SERVER_ERROR 

ALL LIST:

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