Node.js 快速恢复标头事件不再触发
我有一个中间件功能,可以有效地传递,但用于记录响应状态代码、开始时间、结束时间等。在某个时候,此代码停止工作,可能是在升级到Express 4之后Node.js 快速恢复标头事件不再触发,node.js,express,Node.js,Express,我有一个中间件功能,可以有效地传递,但用于记录响应状态代码、开始时间、结束时间等。在某个时候,此代码停止工作,可能是在升级到Express 4之后 module.exports = function () { var app = express(); app.use(function (req, res, next) { res.on('header', function () { // Here I could inspect res.headers, res.s
module.exports = function () {
var app = express();
app.use(function (req, res, next) {
res.on('header', function () {
// Here I could inspect res.headers, res.statusCode, etc.
// This 'header' event seems to no longer be fired.
}
next();
});
return app;
}
我也尝试过使用express.Router()
而不是express()
,但在行为上没有区别
这个功能消失了吗?在其他一些中间件发送响应之后,但在响应正文结束之前,是否有另一种解决方案来获取响应标头?
header
事件由Connect
中间件触发
从ExpressJS Wiki引用
Express 4不再将Connect作为依赖项。这意味着
捆绑中间件(静态除外)不再在上可用
快速模块。每个中间件都作为一个模块提供。(更多关于
这将在下面列出。)
此更改允许中间件接收修复、更新和发布,
不影响快速发布周期(反之亦然)
来源:事实证明,响应对象上的
头事件是由Connect触发的。此事件在Connect 2.x中已被弃用,并在Connect 3.x中删除。从:
建议的模块:谢谢@Sobiaholic。事实证明,此事件也已从Connect中删除。有一种被大量使用的鸭子冲床,作为替代品,似乎经过了很好的测试:
res.on = function(type, listener){
if (type === 'header') {
deprecate('res.on("header"): use on-headers npm module instead');
onHeaders(this, listener);
return this;
}
return addListener.apply(this, arguments);
};