JSendie is an Express middleware to provide structured JSON reponses
with 3 new methods - res.success
, res.fail
, and res.error
.
It is based on JSend specification (http://labs.omniti.com/labs/jsend).
npm install jsendie
Setup the middleware in your Express app. Before any routes.
var express = require('express')
, jsendie = require('jsendie')
, app = express()
, PORT = 3000;
// Remmeber to use it before any routes
app.use(jsendie());
app.get('/success', function (req, res){
res.success({
message: 'Hi, JSendie!'
});
});
app.listen(PORT, function(){
console.log('JSendie listen to port: ' + PORT)
});
// http://localhost:3000/success response body
// {
// "status": "success",
// "data": {
// "notice": "Hi, JSendie!"
// }
// }
https://github.com/kahwooi/jsendie/blob/master/test/server.js
JSendie adds the following methods to the res
methods:
- data: Required success data.
- data: Required failure data. If the reasons for failure correspond to POST values, the response object's keys SHOULD correspond to those POST values.
- message: Required error message.
- data: Optional error data.
- code: Optional error code.
This project is licensed under the MIT license. See the LICENSE file for more info.