node.js使用http.request请求别的服务器数据时,有时会报错err:socket hands up ! 当socket hands up 过多时又会报错: unhandled exception:Error: accept EMFILE at errnoException (net.js:769:11) at TCP.onconnection (net.js:1017:24) 直接导致程序必须重启。 求解!设置请求超时时间的时候,当请求超时时候调用abort()或者destroy()释放还是会报错err:socket hands up ! socket hands up是什么原因? 具体代码如下: function(req, res) {
transtr='*******';
var options = {
host : 'xxxxxxxxxxxx',
port : 80,
path : '/ajax/requestHead.do',
data : transtr,
method : 'POST',
headers : {
'Content-Type' : 'application/x-www-form-urlencoded',
'Content-Length' : transtr.length,
'Connection':'close'
}
};
console.log(transtr+'----------------------------------------');
var request_timer = null, serareq = null;
var serareq = http.request(options, function(serares) {
var heads = serares.headers;
serares.setEncoding('utf8');
clearTimeout(request_timer);
// 等待响应60秒超时
var response_timer = setTimeout(function() {
serares.destroy();
console.log('Response Timeout.');
}, 60000);
console.log("Got response: " + res.statusCode);
var resData= '';
serares.on('data', function (respData) {
resData += respData;
});
serares.on('end', function () {
clearTimeout(response_timer);
log.logger.info('=======rData============================')
res.send(resData);
});
});
serareq.on("timeout", function() {
//res.send('');
log.logger.info("timeout received");
if (serareq.serares) {
serareq.serares.emit("abort");
}
serareq.abort();
});
//设置请求超时时间5秒
request_timer = setTimeout(function() {
//serareq.abort();
serareq.emit("timeout",{message:'have been timeout...'});
log.logger.info('=====Request Timeout.=================');
}, 5000);
log.logger.info("Send packageData!!!!!!!!!!!!!!!!");
// write data to request body
serareq.write(transtr);
serareq.end();
}