Express res header事件不再发射

我有一个中间件function,它是有效的通过,但用于logging响应状态代码,开始时间,结束时间等。在某些时候,代码停止工作,可能升级到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.statusCode, etc. // This 'header' event seems to no longer be fired. } next(); }); return app; } 

我也尝试使用express.Router()而不是express() ,但行为没有区别。

这个function消失了吗? 有其他的中间件发送响应之后,但响应正文结束之前 ,是否有其他解决scheme来获取响应头文件?

header事件是由Connect中间件触发的。

从ExpressJS Wiki引用

Express 4不再具有连接作为依赖项。 这意味着所有捆绑的中间件(静态除外)在express模块​​上不再可用。 每个中间件都可以作为一个模块使用。 (更多关于这个下面。)

此更改允许中间件接收修补程序,更新和发行版,而不会影响Express发行周期(反之亦然)。

来源: https : //github.com/strongloop/express/wiki/Migrating-from-3.x-to-4.x

事实certificate,响应对象的header事件被激发连接。 在Connect 2.x中,此事件已弃用,并从Connect 3.x中删除。 从Connect 2.x源代码 :

  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); }; 

build议的模块: https : //www.npmjs.org/package/on-headers