I want to send a custom json response on each request at the end like:
{
"message": "Something",
"type": "success",
"payload": /*object, array, anything else */
}
What I can do is at the end of each request use:
res.status(200).json({message: 'Something', type: 'success', payload});
If for some reason I want my responses to have another format, I have to go and change each res.json, which is bad.
But since I know all my responses will have this format, is there any way to create a middleware and pass some variables like payload, message, type which gets executed at the end of each request?