Ami*_*oda 13 rest timeout node.js npm
以下将是我的node.js调用以检索一些数据,这需要超过1分钟.这将是1分钟(60秒)的超时.我还为延迟设置了一个控制台日志.但是我已将超时配置为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);
});
Run Code Online (Sandbox Code Playgroud)
Mat*_*tis 10
从请求选项文档中,向下滚动到timeout条目:
timeout - 整数,包含在中止请求之前等待服务器发送响应头(并启动响应主体)的毫秒数.请注意,如果无法建立基础TCP连接,则OS范围的TCP连接超时将取代超时选项(Linux中的默认值可以是20-120秒).
请注意最后一部分"如果无法建立基础TCP连接,则OS范围的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');
}
});
Run Code Online (Sandbox Code Playgroud)
如果这是真的,您需要决定是否可以更改操作系统设置(这超出了本答案的范围,这样的问题在服务器故障上会更好).