My question is actually copied from Proper way to return JSON using node or Express. I need the response in this format.
Sample format for response API
{
"success":true,
"code":200,
"message":"Ok",
"data": []
}
I followed all the methods provided in the above question, but still i couldn't crack the right answer. since i have many apis, i need this response format for every api.
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
if (req.method === "OPTIONS") {
res.header("Access-Control-Allow-Methods", "POST, DELETE, GET");
return res.status(200).json({});
}
next();
});
app.use("/api", employeeRoutes);
app.use("/api", groupRoutes);
app.use((req, res, next) => {
const error = new Error("Not found");
error.status = 404;
next(error);
});
the above snippet is of my app.js file. And my route code looks somethig like this.
exports.groups_Get_All = (req, res, next) => {
Group.find()
.exec()
.then(docs => {
const response =
docs.map(doc => {
return {
gname: doc.gname,
employee: doc.employeeId,
_id: doc._id,
createdAt: doc.createdAt
};
})
res.send((response));
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
};
Right now im getting response of just plain data in json format.
[
{
"gname": "wordpres",
"employee": [
"5c6568102773231f0ac75303"
],
"_id": "5c66b6453f04442603151887",
"createdAt": "2019-02-15T12:53:25.699Z"
},
{
"gname": "wordpress",
"employee": [
"5c6568102773231f0ac75303"
],
"_id": "5c66cbcf1850402958e1793f",
"createdAt": "2019-02-15T14:25:19.488Z"
}
]
Now my question is how to implement this sample format response to every api(global scope)?
If you are using express, don't send the message from the controller. Make a middleware that's main purpose is to send the response to the client. This will give you a power of setting the format of consist response to the client.
For Example I have made the response middleware like this :-
module.exports = function(req, res, next) {
const message = {};
message.body = req.responseObject;
message.success = true;
message.status = req.responseStatus || 200;
res.status(message.status).send(message);
return next();
};
Above code will generate the format like this.
{
"success": true,
"status": 200,
"body": {
"name": "rahul"
}
}
You can use request uplifter property of express. You can add responseObject and responseStatus from previous middleware.
Errors can be made in separate middleware likewise.
You can call by this in your routes:-
const responseSender = require('./../middleware/responseSender');
/* your rest middleware. and put responseSender middleware to the last.*/
router.get('/',/* Your middlewares */, responseSender);
You can call it by:-
exports.groups_Get_All = (req, res, next) => {
Group.find()
.exec()
.then(docs => {
const response =
docs.map(doc => {
return {
gname: doc.gname,
employee: doc.employeeId,
_id: doc._id,
createdAt: doc.createdAt
};
})
req.responseObject = response; // This will suffice
return next()
})
.catch(next);
}