假设我有多个地方可以打电话response.send(someData)
。现在我想创建一个全局拦截器来捕获所有.send
方法并进行一些更改someData
。 express.js 有什么办法吗? (钩子、监听器、拦截器……)?
您可以定义一个中间件,如下所示(从中获取和修改answer https://stackoverflow.com/questions/19215042/express-logging-response-body/19215370#19215370)
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)