Home > Web Front-end > JS Tutorial > node.js timeout detailed explanation_node.js

node.js timeout detailed explanation_node.js

WBOY
Release: 2016-05-16 16:30:10
Original
1722 people have browsed it

If the server does not respond within the specified time (it may be a problem with the connection between the networks, or it may be because the server fails or the network firewall blocks the connection between the client and the server), the response times out and http is triggered at the same time. The timeout event of the ServerResponse object.

response.setTimeout(time,[callback]);

You can also not specify the callback function in setTimeout. You can use time monitoring to specify the callback function.

If a timeout callback function is not specified, then a timeout occurs, the socket port connected to the http client will be automatically closed. If a timeout callback function is specified, then a callback function will be called instead of a timeout. The socket port connected to the http client will be automatically closed.

Copy code The code is as follows:

var http=require("http");
var server=http.createServer(function(req,res){
If(req.url!=="/favicon.ico"){
//Timeout monitoring
​​​​/*res.setTimeout(1000);
         res.on("timeout",function(){
console.log("Response timeout.");
         });*/
// Direct callback when timeout
          res.setTimeout(1000,function(){
console.log("Response timeout.");
        });
setTimeout(function(){
              res.setHeader("Content-Type", "text/html");
res.write("");
                res.write("Hello");
               res.end();
         },2000);
}
});
server.listen(1337,"localhost",function(){
console.log("Start monitoring" server.address().port "...");
});

Result of running the code:

After deleting the timeout callback function:

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