node.js http server, detect when clients disconnec

2019-03-14 23:31发布

问题:

I am using express with node.js for a http server. I store the response object so I can stream events down to the client on that channel. Is there a way to detect when the client disconnects? When I kill my client I can still write to the response object without getting any kind of exception/error. It looks like the service keeps the underlying tcp channel open as long as I keep writing to the response object.

回答1:

req.on("close", function() {
  // request closed unexpectedly
});

req.on("end", function() {
  // request ended normally
}):