Before the end method of the http.ServerResponse object is called, if the connection is interrupted, the close event of the http.ServerResponse object will be triggered.
The code copy is as follows:
var http=require("http");
var server=http.createServer(function(req,res){
if(req.url!=="/favicon.ico"){
res.on("close",function(){
console.log("Connection interrupt")
});
setTimeout(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("Start listening"+server.address().port+"...");
});
The above code looks like this:
When the client requests, after 10 seconds, send "Hello" to the client and listen for the close event at the same time.
As long as the server is closed within 10 seconds, the server will have "connection interrupted", because the res.end() method will not be executed within 10 seconds.