node.js 连接或 Express 中间件来修改 response.body
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9896628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect or Express middleware to modify the response.body
提问by The Who
I would like to have a middleware function which modifies the response body.
我想要一个修改响应正文的中间件功能。
This is for an express server.
这是用于快速服务器。
Something like:
就像是:
function modify(req, res, next){
res.on('send', function(){
res.body = res.body + "modified"
});
next();
}
express.use(modify);
I don't understand what event to listen for. Any help or documentation would be appreciate.
我不明白要听什么事件。任何帮助或文档将不胜感激。
采纳答案by Christopher Tarquini
You don't need to listen to any events. Just make it
您不需要监听任何事件。做吧
function modify(req, res, next){
res.body = res.body + "modified";
next();
}
And useit after you usethe router. This way after all your routes have executed you can modify the body
而use之后你把它use的路由器。这样在您执行完所有路线后,您就可以修改正文
回答by Corey Jewett
I believe the OP actually wants to modify the response stream once a middleware has handled the request. Look at the bundled Compressmiddleware implementation for an example of how this is done. Connect monkey patches the ServerResponseprototype to emit the headerevent when writeHeadis called, but before it is completed.
我相信一旦中间件处理了请求,OP 实际上想要修改响应流。查看捆绑的Compress中间件实现以了解如何完成此操作的示例。连接猴子修补ServerResponse原型以header在writeHead调用时但在完成之前发出事件。
回答by Richard Schneider
express-mungis designed for this. Instead of events its just more middleware. Your example would look something like
express-mung就是为此而设计的。而不是事件,它只是更多的中间件。你的例子看起来像
const mung = require('express-mung')
module.exports = mung.json(body => body.modifiedBy = 'me');
回答by Peleg
Overwriting the response's writemethod seemed to work for me with Express 4. This allows modifying the response's body even when it's a stream.
使用 Express 4覆盖响应的write方法似乎对我有用。这允许修改响应的主体,即使它是一个流。
app.use(function (req, res, next) {
var write = res.write;
res.write = function (chunk) {
if (~res.getHeader('Content-Type').indexOf('text/html')) {
chunk instanceof Buffer && (chunk = chunk.toString());
chunk = chunk.replace(/(<\/body>)/, "<script>alert('hi')</script>\n\n");
res.setHeader('Content-Length', chunk.length);
}
write.apply(this, arguments);
};
next();
});
Just make sure to register this middleware before any other middleware that may be modifying the response.
只需确保在可能修改响应的任何其他中间件之前注册此中间件。
回答by Greg Slepak
There seems to be a module for doing just this called connect-static-transform, check it out:
似乎有一个模块可以执行此操作connect-static-transform,请查看:
https://github.com/KenPowers/connect-static-transform
https://github.com/KenPowers/connect-static-transform
A connect middleware which allows transformation of static files before serving them.
一个连接中间件,允许在提供静态文件之前对其进行转换。
And it comes with examples, like this one.
它附带了一些例子,比如这个。

