Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I capture a "response end" event in node.js+express?

I'd like to write an express middleware function that sets up a listener on the response's 'end' event, if one exists. The purpose is to do cleanup based on the http response code that the end handler decided to send, e.g. logging the response code and rollback/commit of a db transaction. i.e., I want this cleanup to be transparent to the end caller.

I'd like to do something like the following in express:

The route middleware

function (req, res, next) {    res.on ('end', function () {       // log the response code and handle db       if (res.statusCode < 400) { db.commit() } else { db.rollback() }    });    next(); } 

The route:

app.post ("/something", function (req, res) {      db.doSomething (function () {        if (some problem) {           res.send (500);        } else {           res.send (200);        }     });  } 

When I try this, the 'end' event handler never gets called. The same for res.on('close'), which I read about in another post. Do such events get fired?

The only other way I can think of doing this is wrapping res.end or res.send with my own version in a custom middleware. This is not ideal, because res.end and res.send don't take callbacks, so I can't just wrap them, call the original and then do my thing based on the response code that got set when they call me back (because they won't call me back).

Is there a simple way to do this?

like image 407
Jake Avatar asked Jun 21 '12 11:06

Jake


People also ask

How do you end a response in node JS?

end() method ends the current response process. This method is used to quickly end the response without any data.

What is Post () in Express?

js POST Method. Post method facilitates you to send large amount of data because data is send in the body. Post method is secure because data is not visible in URL bar but it is not used as popularly as GET method. On the other hand GET method is more efficient and used more than POST.

What is the function of response end listener?

The purpose is to do cleanup based on the http response code that the end handler decided to send, e.g. logging the response code and rollback/commit of a db transaction.


2 Answers

Strangely enough, it appears that the response emits a "finish" event when the response is closed: https://web.archive.org/web/20120825112648/http://sambro.is-super-awesome.com/2011/06/27/listening-for-end-of-response-with-nodeexpress-js/

Despite this blog entry being a bit old, this event still exists (Line 836 in lib/http.js), so I assume it won't disappear soon, even though neither node's nor express' documentation mention it. By early 2014 it has moved to line 504 on of lib/_http_outgoing.js and still works.

By the way, the "error" event on a server response is probably not something you'd usually want to see.

like image 148
Aveius Avatar answered Sep 27 '22 17:09

Aveius


Just to further @Amatsuyu's response, here's how it should look:

res.on('finish', function() {   // do stuff here }); 
like image 28
dmon Avatar answered Sep 27 '22 19:09

dmon