This plugin will help you configure your response messages easily, in ExpressJS
Specify the buildRes middleware, after specifying all the other route handlers.
app.use(index, '/');
app.use(users, '/users');
...
app.use(require('./lib/buildRes'));
Now, specify the response's configuration in the respose object, as a property buildRes
, and then call next()
...
res.buildRes({
// Configurations
});
next();
status
: 200headers
: {}message
: Accorging to the status code
router.get('/', function(req, res, next) {
// Configuration object
res.buildRes = {
status:404,
headers: {
header1: 'firstHeader',
header2: 'secondHeader'
},
message: 'Sorry, not found'
};
// Call next()
next();
});