在nodejs中进行http请求,设置超时,如果是req.abort() 那么http 请求还会是继续的,但是re.destory()就会彻底中止,他们详细区别是什么?
还有个超时问题,分为请求超时和响应超时,以前没这个概念,最近才认识到这个细节,我想问问这两个分别意味着什么?测试一个资源快慢,应该以哪个为标准?
var http = require('http');
var request_timer = null, req = null;
request_timer = setTimeout(function() {
req.destroy();
console.log('Request Timeout.');
console.log('1');
}, 1000);
// 请求5秒超时
var options = {
host: 'www.baidu.com',
port: 80,
path: '/'
}
req = http.get(options, function(e) {
clearTimeout(request_timer);
}).on('error', function(err) {
if(request_timer) {
clearTimeout(request_timer);
console.log('2');
}
console.log('3');
});
假设 timeout设置为 1ms(测试强制超时),req.destroy()的话 超时输出到3,直接退出,但是如果是req.abort()的话,等到输出3会过一会才会退出,按照nightire的说法,可能是请求已经发出,等待响应,所以req.abort()是没有影响的。
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
业精于勤,荒于嬉;行成于思,毁于随。