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

后端 未结 4 2185
我寻月下人不归
我寻月下人不归 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:24

    for those finding on google, based off the top answer:

    app.use((req, res, next) => {
        let oldSend = res.send
        res.send = function(data) {
            console.log(data) // do something with the data
            res.send = oldSend // set function back to avoid the 'double-send'
            return res.send(data) // just call as normal with data
        }
        next()
    })
    

提交回复
热议问题