0

I am working in a Node.js RESTful API project with Express framework.
Every response of APIs has to include the "status" field in the response body, also in the header.
I think that I may have to use a middleware(Express Middleware) to achieve this goal, not containing the "status" by:

res.send({status:200, ...})
From the documentation I understood that there is a middleware to pre-process the requests.
Even I found a question:Connect or Express middleware to modify the response.body but it is to add a normal data.

But I am not sure if there is a middleware to add a specific field to the response body from catching the specific field in the field, the after processing the api requests.

I need your help. Thanks!

Community
  • 1
  • 1
  • 1
    Possible duplicate of [Connect or Express middleware to modify the response.body](http://stackoverflow.com/questions/9896628/connect-or-express-middleware-to-modify-the-response-body) – imjared Aug 22 '16 at 03:51

1 Answers1

0

You can just write your own middleware to do it. Here's a simple inline middleware (not refactored to use modules):

app.use(function(req, res, next) {
  res.status(200);
  next();
});

The above sets the status code to 200 as default to all handlers.

However, I don't think you need to do this as express by default assume a 200 status response. If you find yourself needing to do this then you've got some middleware that's setting the status code to something else.

slebetman
  • 109,858
  • 19
  • 140
  • 171