0

I have one question.

In my current application I am using express, and I and using Express.get and Express.post method multiple times because I have multiple pages.

So, I want to intercept each response before sending it to client to add some state in response object.

Is there any way in express.js by which I can intercept each response before sending it to client.

Abhishek Jain
  • 285
  • 1
  • 3
  • 12
  • Just do whatever it is you want to do to the data inside the middleware function before sending it to the client. – JJJ Nov 04 '17 at 18:40
  • I have multiple get/post method, and I need a single point where I can modify the response data... despite of how many express controllers I have.. Mainly I need to intercept every controller response at one place. – Abhishek Jain Nov 04 '17 at 18:43
  • 1
    You can write a separate middleware for this purpose and use it without giving a path like `app.use(myNewMiddleware)`. It will be applied for all requests, and will be independent of any get/post route middleware you have. Just be careful about the order. – Vasan Nov 04 '17 at 18:56
  • Yes. I agree, but this only handles when user request comes to express not at the time when response going back to client... – Abhishek Jain Nov 04 '17 at 18:59
  • I don't know your exact requirement. But it should work for responses too. See here for an example of what I am referring to: https://stackoverflow.com/questions/31661449/express-js-how-to-set-a-header-to-all-responses – Vasan Nov 04 '17 at 19:02
  • may be duplicate of https://stackoverflow.com/questions/27885425/express-middleware-before-response-is-carried-out-to-client – Jagdish Idhate Nov 05 '17 at 05:09

0 Answers0