[![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url]
$ npm install express-res
var responses = require('express-res');
Create a express response middleware with the given options
.
The default custom express mvc response middleware path is ./app/responses/
var express = require('express')
var responses = require('express-res');
var app = express()
app.use(responses());
Create a file in ./app/responses folder,this call ok.js.
The content is like:
var _ = require('lodash');
module.exports = function ok (data, options) {
var req = this.req;
var res = this.res;
var defaultOptions = {
keepEmptyData:false
};
options = _.assign(defaultOptions,options);
var json = {
code:200,
msg:'ok',
data:data
};
if(!options.keepEmptyData && _.isEmpty(json.data)){
delete json.data;
}
res.json(json);
};
So when express mvc app start.The responses middleware setup auto. Then, you can use custom method on res you define.
router.get('/test/ok',function(req,res){
res.ok({name:"jerry wu"});
});