假设我有多个调用 response.send(someData)
的地方。现在我想创建一个全局拦截器,我可以在其中捕获所有 .send
方法并对 someData
进行一些更改。 express.js 有什么办法吗? (钩子(Hook)、监听器、拦截器……)?
最佳答案
您可以如下定义一个中间件(取自并修改自 answer )
function modifyResponseBody(req, res, next) {
var oldSend = res.send;
res.send = function(data){
// arguments[0] (or `data`) contains the response body
arguments[0] = "modified : " + arguments[0];
oldSend.apply(res, arguments);
}
next();
}
app.use(modifyResponseBody);
关于javascript - express.js - 如何拦截 response.send()/response.json(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33732509/