Express res标头事件不再触发 [英] Express res header event no longer firing

查看:68
本文介绍了Express res标头事件不再触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中间件功能,该功能可以有效传递,但用于记录响应状态代码,开始时间,结束时间等.有时在升级到Express 4之后,该代码停止工作.

I have a middleware function that was effectively pass-through, but was used for logging response status codes, start time, end time, etc. At some point this code stopped working, likely after upgrading to 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(),但是在行为上没有区别.

I have also tried using express.Router() instead of express(), but there is no difference in behavior.

此功能消失了吗?有没有其他的解决方案,可以在其他中间件发送响应之后之后,但在响应主体结束之前之前,获取响应头?

Is this functionality gone? Is there an alternative solution to getting the response headers after some other middleware has sent a response, but before the response body ends?

推荐答案

事实证明,响应对象上的header事件已触发Connect.此事件在Connect 2.x中已弃用,并从Connect 3.x开始删除.从连接2.x源代码:

It turns out that the header event on response objects was fired up Connect. This event was deprecated in Connect 2.x, and removed as of Connect 3.x. From the Connect 2.x source code:

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

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

这篇关于Express res标头事件不再触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆