如何检测客户端closures连接期间通过http在pipe道stream节点?

我使用expressjs(nodejs)应用程序将数据从服务器传输到客户端。

我做这样的事情;

// let contentType and filename be correctly defined in context function(req,res){ res.setHeader('Content-Type',contentType); res.setHeader('Content-Disposition','attachment; filename='+filename); require('fs').createReadStream('/path/file').pipe(res); } 

我想知道客户端何时无法下载文件,但我不知道如何继续。

听响应对象的close事件。 当与客户端的连接过早closures时,如刷新页面或closures选项卡,会发出此消息。

 require('fs').createReadStream('/path/file').pipe(res).on('close',function(){ //do stuff for closed connection })