I am trying to create middleware that records response time and status codes and sends it to the database. However, I am not sure which event to use. There is a close event in node documentation, but it never fires. end doesn't work either. However, header does, but I cannot 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 the header executed and it returns the correct res.statusCode file.
My questions:
- Why is there no
close arrow? Why shooting header ? - Is this a reliable way?
source share