jSend
Express middleware for jSend responses.
install
With npm do:
npm install proto-jsend
Then assign as express middleware. This will add the jSend methods to the express request and response objects.
var app = jSend = ; app;
Methods
res.jSend
/** * Send jSend success response to the client * * res.jSend will infer the status code (200 or 201 from the request http method) * if jsonp is used req.method is always 'GET' so in this case req._method property must be set to the desired method e.g 'POST' if you wish to receive a 201 code * * @param */res { // ...}
res.jSend.error
- This is for 5xx type errors (i.e. internal server error)
/** * Send custom jSend "error" messages to client * * @param * @param * @param * @param */resjSend { // ...}
- Example usage:
resjSend;
HTTP/1.1 500 Error{ "status": "error", "message": "Internal Server Error"}
- A stack trace (or I guess, whatever you want to put in data) will also show on the data property when not in production environment for debugging purposes:
HTTP/1.1 500 Error{ "status": "error", "message": "Internal Server Error", "data": "Error: Broke it!\n at Object.<anonymous> (/Users/foo/app.js:12:25)\n at Module._compile (module.js:460:26)\n at Object.Module._extensions..js (module.js:478:10)\n at Module.load (module.js:355:32)\n at Function.Module._load (module.js:310:12)\n at Function.Module.runMain (module.js:501:10)\n at startup (node.js:129:16)\n at node.js:814:3"}
res.jSend.fail
- This is for 4xx type errors (i.e. bad request, unauthorised etc.)
- Footprint:
/** * Send custom jSend "fail" messages to client * * @param * @param * @param */resjSend { // ...};
- Example usage:
resjSend;
HTTP/1.1 400 Bad Request{ "status": "fail", "data": { "title": "Title property of type string is required" }}