Home > Web Front-end > JS Tutorial > body text

A brief analysis of the close event in node.js_node.js

WBOY
Release: 2016-05-16 16:30:09
Original
1316 people have browsed it

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.

Copy code The 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 monitoring" server.address().port "......");
});

The above code is like this:

When the client makes a request, send "Hello" to the client after 10 seconds. At the same time, listen for the close event.

As long as the server is shut down within 10 seconds, "Connection interrupted" will appear on the server because the res.end() method will not be executed within 10 seconds.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template