gpt4 book ai didi

javascript - express.js - 如何拦截 response.send()/response.json()

转载 作者:可可西里 更新时间:2023-11-01 01:21:02 24 4
gpt4 key购买 nike

假设我有多个调用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com