http.get 怎么捕获timeout(网络超时)事件?
平时是这样
http.get(indexUrl, function(res) {
var body = [];
res.on('data', function(chunk) {
body.push(chunk);
});
res.on('end', function(chunk) {
//todo
});
//问题在这里
res.on('什么' 能捕获到timeou(超时)
});
7 回复
http.get(indexUrl, function(res) {
var body = [];
res.on('data', function(chunk) {
body.push(chunk);
});
res.on('end', function(chunk) {
//todo
});
//问题在这里
res.on('什么' 能捕获到timeou(超时)
}).setTimeout(1000, function(){
console.log('timeout!');
});
哦~貌似不是我想的这样~
应该是
- req.setTimeout, 然后实际是 socket.setTimeout, see https://nodejs.org/dist/latest-v4.x/docs/api/http.html#http_request_settimeout_timeout_callback
- 为 socket 监听 timeout 事件 see
req.setTimeout(3000);
req.on('socket', function(socket){
socket.on('timeout', function(){
// timeout exceed
});
});