An Analysis of the close event in node. js and node. jsclose
If the connection is interrupted before the end method of the http. ServerResponse object is called, the close event of the http. ServerResponse object is triggered.
Copy codeThe Code 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 interrupted ")
});
SetTimeout (function (){
Res. setHeader ("Content-Type", "text/html ");
Res. write ("Res. write ("hello ");
Res. end ();
},10000 );
}
});
Server. listen (1337, "localhost", function (){
Console. log ("Start listening" + server. address (). port + "......");
});
The above code is as follows:
After a request is sent from the client, send "hello" to the client after 10 seconds. Listen for the close event at the same time.
If the server is shut down within 10 seconds, the server will see "the connection is interrupted" because the res. end () method will not be executed within 10 seconds.