NPM请求模块(REST客户端)的默认超时是什么?

以下将我的node.js调用来检索一些数据,这需要超过1分钟。 这将在1分钟(60秒)超时。 我把一个控制台日志的延迟也。 不过,我已经configuration超时120秒,但它不反映。 我知道默认级别的nodejs服务器的超时时间是120秒,但我仍然从这个请求模块获取此次调用的超时时间(60秒)。 请提供你的见解。

var options = { method: 'post', url:url, timeout: 120000, json: true, headers: { "Content-Type": "application/json", "X-Authorization": "abc", "Accept-Encoding":"gzip" } } var startTime = new Date(); request(options, function(e, r, body) { var endTime = new Date(); var latencyTime = endTime - startTime; console.log("Ended. latencyTime:"+latencyTime/1000); res.status(200).send(body); }); 

从请求选项文档中 ,滚动到timeout条目:

timeout – 整数,包含等待服务器发送响应头(并启动响应正文)的毫秒数,然后中止请求。 请注意,如果无法build立底层的TCP连接,OS范围的TCP连接超时将会取消超时选项(Linux中的默认值可以在20-120秒之间)。

请注意最后一部分“如果无法build立底层的TCP连接,那么操作系统范围的TCP连接超时将超过超时选项”。

还有一个关于超时的完整部分。 基于这个和你的代码示例,我们可以修改请求示例

 request(options, function(e, r, body) { if (e.code === 'ETIMEDOUT' && e.connect === true){ // when there's a timeout and connect is true, we're meeting the // conditions described for the timeout option where the OS governs console.log('bummer'); } }); 

如果这是真的,你需要决定是否可以改变操作系统设置(这超出了这个答案的范围,这样的问题会更好的服务器故障)。