nodejs中,request库在https+proxy环境下产生Error: ESOCKETTIMEDOUT。
更多分类文章:
Python | Linux | 大数据 | 数据库 | Git | Nodejs | Golang | 电脑技巧 | 计算机技术
错误描述
{ Error: ESOCKETTIMEDOUT
at ClientRequest.<anonymous> (D:\codeProject\nodejs-webCrawler\webCrawler-nodeCrawler\node_modules\request\request.js:812:19)
at Object.onceWrapper (events.js:312:19)
at emitNone (events.js:105:13)
at ClientRequest.emit (events.js:207:7)
at Socket.emitTimeout (_http_client.js:720:34)
at Object.onceWrapper (events.js:312:19)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at Socket._onTimeout (net.js:401:8)
at ontimeout (timers.js:488:11) code: 'ESOCKETTIMEDOUT', connect: false }
调用处代码
// in tunnel-agent.js
connectReq.once('connect', onConnect)
//...
function onConnect(){
pending.request.onSocket(socket) // pending.request is an instance of ClientRequest
}
// in _http_client.js
function onSocket(socket){
req.emit('socket',socket); // request.js got the 'socket' event.
}
// in request.js
self.req.on('socket', function(socket) {
var setReqTimeout = function() {
self.req.setTimeout(timeout, function () {
if (self.req) {
self.abort()
var e = new Error('ESOCKETTIMEDOUT')
e.code = 'ESOCKETTIMEDOUT'
e.connect = false
self.emit('error', e)
}
})
}
}
解决方法
添加如下代码:
connectReq.setTimeout(options.timeout || 15000, function(){
connectReq.abort();
});