Reputation: 163538
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;
}
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?
Upvotes: 2
Views: 3808
Reputation: 163538
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);
};
The suggested module: https://www.npmjs.org/package/on-headers
Upvotes: 3
Reputation: 2967
header
event is fired by Connect
middleware.
Quoting from ExpressJS Wiki
Express 4 no longer has Connect as a dependency. This means that ALL bundled middleware (except static) is no longer available on the express module. Each middleware is available as a module. (More on this below.)
This change allows middleware to receive fixes, updates, and releases, without impacting Express release cycles (and vice-versa).
Source: https://github.com/strongloop/express/wiki/Migrating-from-3.x-to-4.x
Upvotes: 2