express.js - how to intercept response.send() / response.json()

后端 未结 4 2166
我寻月下人不归
我寻月下人不归 2020-12-08 14:57

Lets say I have multiple places where I call response.send(someData). Now I want to create a single global interceptor where I catch all .send meth

4条回答
  •  一向
    一向 (楼主)
    2020-12-08 15:33

    Yes this is possible. There are two ways to do this, one is to use a library that provides the interception, with the ability to run it based on a specific condition: https://www.npmjs.com/package/express-interceptor

    The other option is to just create your own middleware (for express) as follows:

    function modify(req, res, next){
      res.body = "this is the modified/new response";
    
      next();
    }
    express.use(modify);
    

提交回复
热议问题