Kishore Indraganti
Kishore Indraganti

Reputation: 1322

Middleware triggering once response send

I am trying to trigger some function once after response has been sent to client for that am using the following code, But res.on('end' not triggering at all.So what I am missing here, or else any other efficient way to achieve this?

 app.use(function(req, res, next) {
      res.on('end', function() {
        console.log('end') ; //Portsion need to execute once response send
      })

      next();
    })

Upvotes: 2

Views: 421

Answers (1)

Manuel Miranda
Manuel Miranda

Reputation: 823

In the last versions of node.js the correct event to watch for end or close actions is finish (documentation). So your code should be

app.use(function(req, res, next) {
  res.on('finish', function() {
    console.log('end') ;
  })

  next();
})

Upvotes: 3

Related Questions