zoukankan      html  css  js  c++  java
  • node的超时timeout

    如果在指定的时间内服务器没有做出响应(可能是网络间连接出现问题,也可能是因为服务器故障或网络防火墙阻止了客户端与服务器的连接),则响应超时,同时触发http.ServerResponse对象的timeout事件.

    response.setTimeout(time,[callback]);

    也可以不在setTimeout中指定回调函数,可以使用时间的监听的方式来指定回调函数.

    如果没有指定超时的回调函数,那么出现超时了,将会自动关闭与http客户端连接的socket端口.如果指定了超时的回调函数,那么超时了,将会出现调用回调函数,而不会自动关闭与http客户端连接的socket端口.

     1 var http=require("http");
     2 var server=http.createServer(function(req,res){
     3     if(req.url!=="/favicon.ico"){
     4         //超时监听
     5         /*res.setTimeout(1000);
     6         res.on("timeout",function(){
     7             console.log("响应超时.");
     8         });*/
     9         //超时直接回调
    10         res.setTimeout(1000,function(){
    11            console.log("响应超时.");
    12         });
    13         setTimeout(function(){
    14             res.setHeader("Content-Type","text/html");
    15             res.write("<html><head><meta charset='utf-8' /></head>");
    16             res.write("你好");
    17             res.end();
    18         },2000);
    19     }
    20 });
    21 
    22 server.listen(1337,"localhost",function(){
    23     console.log("开始监听"+server.address().port+"......");
    24 });

    运行代码结果:

    删除超时的回调函数后:

  • 相关阅读:
    基于Ubuntu Jeos打造自己的精简版Linux服务器
    35 vs 53怎么裁
    父母在,不远游
    linux deepin是基于linux mint修改
    novell
    Sahi
    virtualbox on windows store vdi on ndfs due the file will bigger than 4gb
    在Linux下配置邮件系统
    CSS3 backgroundsize 属性
    dede:list及dede:arclist 按权重排序的方法
  • 原文地址:https://www.cnblogs.com/guoyansi19900907/p/4066470.html
Copyright © 2011-2022 走看看