http.serverresponseオブジェクトの最後のメソッドが呼び出されます。接続が中断されると、http.serverresponseオブジェクトの緊密なイベントがトリガーされます。
コードコピーは次のとおりです。
var http = require( "http");
var server = http.createserver(function(req、res){
if(req.url!== "/favicon.ico"){
res.on( "close"、function(){
console.log( "接続割り込み")
});
setimeout(function(){
res.setheader( "content-type"、 "text/html");
res.write( "<html> <head> <meta charset = 'utf-8' /> < /head>");
res.write( "hello");
res.end();
}、10000);
}
});
server.listen(1337、 "localhost"、function(){
console.log( "聴取を開始"+server.address()。ポート+"...");
});
上記のコードは次のようになります:
クライアントが10秒後にリクエストしたら、クライアントに「Hello」を送信し、同時にクローズイベントを聞いてください。
サーバーが10秒以内に閉じられている限り、Res.End()メソッドが10秒以内に実行されないため、サーバーは「接続中断」されます。