Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 446 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I capture a "response end" event in node.js+express?

#1
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?
Reply

#2
Reading the [documentation][1], here is the signature of `res.send`:

res.send(body|status[, headers|status[, status]])

Which means you can set your own status, like this: `res.send( 'some string', 200 );` or even just `res.send( 404 );`.

This method is the one you use to *send* the response.

Once it is sent to the client, you can't access it anymore, so there is no callback.

**This is the last thing your server does. Once it has processed the request, it sends the response.**

However, you can access it *before* you send it to the client. Which means you can:

console.log( res );
res.send( datas );

If you want to rollback/commit, you do it when the database's callback is called, not when the response is gone.


[1]:

[To see links please register here]

Reply

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

res.on('finish', function() {
// do stuff here
});

Reply

#4
Strangely enough, it appears that the response emits a "finish" event when the response is closed:

[To see links please register here]


Despite this blog entry being a bit old, this event still exists ([Line 836 in lib/http.js][1]), 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][2] and still works.

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


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through