2014-01-21 29 views
0
var request = require('request'), async = require('async'); 
var opt=["http://localhost:8080/examples/jsp/abc.jsp","http://192.168.24.180:8080/examples/jsp/abc.jsp"]; 
function req() 
{ 
    var count = 0; 
    async.eachSeries(opt, function(entry, callback) 
    { 
     count++; 
     request(entry, function(error, response, body) 
     { 
      if (!error && response.statusCode == 200) 
      { 
       console.log('Yes ' + entry); 
      } 
      else 
      { 
       console.log('No '+entry); 
      } 
      callback(); 
     }); 
    }, function(err) 
    { 
     console.log('Continue'); 
    }); 
} 
req(); 

如果响应时间超过0.5秒,我该如何显示“否”。服务器上的文件具有随机休眠时间。如果响应时间超过0.5秒,我该如何继续?

回答

1

您使用的是https://github.com/mikeal/request

如果是这样,根据http://www.sitepoint.com/making-http-requests-in-node-js/可以在一个对象传递给条目[请求(条目,函数(ERR],而不是仅仅一个URL,并包括在这一点。

从sitepoint链路

request({ 
    uri: "http://www.sitepoint.com", 
    method: "GET", 

    timeout: 10000, <- that's what you looking for 

    followRedirect: true, 
    maxRedirects: 10 
}, function(error, response, body) { 
    console.log(body); 
}); 
+0

所以,10000是指0.5秒? – damphat

+0

没有,从链路 “超时参数告诉请求等待10000毫秒(10秒)” 所以0.5秒以毫秒为单位将是500个 – brent

+0

单位将是millihalfseconds(MHS) :D他和一个不同的'ETIMEDOUT'错误处理将会诀窍。 – durum

相关问题