I’m trying to create a middleware that logs response times and status codes and sends it to a database. However, I’m not sure what event to use. In node’s documentation there’s a close event but it is never fired. end doesn’t work either. However, header does, but I can’t find any documentation.
app.use(function(req, res, next) {
res.on('close', function() {
console.log('close')
})
res.on('end', function() {
console.log('end')
})
res.on('header', function() {
console.log('header')
console.log(res.statusCode)
})
next()
})
Only header fires, and it does return the correct res.statusCode.
My questions:
- Why isn’t
closefiring? Why isheaderfiring? - Is this a reliable way to go?
closeevent emited only if connection was terminated before response.end() called.headerevent fired byconnect. This is not node.js http.ServerResponse native event.Look at
connectresponseTimemiddleware. I think it should help you.Update:
Here is
headerevent documentation https://github.com/senchalabs/connect/blob/gh-pages/tests.md#patchheaderfired from writeHead method proxied byconnecthttps://github.com/senchalabs/connect/blob/master/lib/patch.js