IncomingMessage中止事件
在Node(4.2.3)中有这个基本的express(4.13.3)服务器。
//blah blah initialization code app.put('/', function(req, res) { req.on('close', function() { console.log('closed'); }); req.on('end', function() { console.log('ended'); }); req.on('error', function(err) { console.log(err); }); res.send(200); });
然后我使用cURL来模拟file upload,如下所示:
curl http://localhost:3000/ -X PUT -T file.zip
它开始上传(虽然没有任何反应),当它结束时,事件end
。
当我用Ctrl + C中止上传时,问题就开始了。 没有事件发生。 什么都没发生。
req
对象从IncomingMessage
inheritance,从而inheritanceReadable
, Stream
和EventEmitter
。
是否有任何事件发生这样的堕胎? 有什么方法可以知道客户端是否中止file upload?
首先编辑:
User @AwalGarg提出了req.socket.on('close', function(had_error) {})
但我想知道是否有任何解决scheme,这是不使用套接字?
从代码中删除res.send(200)
,服务器在客户端上按Ctrl + C时确认closures事件。 等待事件是asynchronous的,但是您立即将响应发送回客户端,从而完成http请求。
在closures事件处理程序,结束事件处理程序和错误事件处理程序中,移动res.send()
(以及任何要发送回客户端的代码)将保持连接处于打开状态,直到其中一个事件被确认。