我正在用Node和Cheerio构建一个网页刮板,对于某个网站,我得到以下错误(它只发生在这一个网站上,没有其他我试图刮的网站。

它每次都发生在不同的位置,所以有时是url x抛出错误,其他时候url x是好的,它是一个完全不同的url:

    Error!: Error: socket hang up using [insert random URL, it's different every time]

Error: socket hang up
    at createHangUpError (http.js:1445:15)
    at Socket.socketOnEnd [as onend] (http.js:1541:23)
    at Socket.g (events.js:175:14)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:910:16
    at process._tickCallback (node.js:415:13)

这是非常棘手的调试,我真的不知道从哪里开始。首先,什么是套接字挂起错误?是404错误还是类似的错误?或者仅仅意味着服务器拒绝连接?

我在任何地方都找不到解释!

编辑:下面是(有时)返回错误的代码示例:

function scrapeNexts(url, oncomplete) {
    request(url, function(err, resp, body) {

        if (err) {
            console.log("Uh-oh, ScrapeNexts Error!: " + err + " using " + url);
            errors.nexts.push(url);
        }
        $ = cheerio.load(body);
        // do stuff with the '$' cheerio content here
    });
}

没有直接调用关闭连接,但我使用节点请求(据我所知)使用http。get所以这是不需要的,如果我错了纠正我!

编辑2:下面是一段实际使用的代码,它会导致错误。prodURL和其他变量主要是前面定义的jquery选择器。这使用了Node的异步库。

function scrapeNexts(url, oncomplete) {
    request(url, function (err, resp, body) {

        if (err) {
            console.log("Uh-oh, ScrapeNexts Error!: " + err + " using " + url);
            errors.nexts.push(url);
        }
        async.series([
                function (callback) {
                    $ = cheerio.load(body);
                    callback();
                },
                function (callback) {
                    $(prodURL).each(function () {
                        var theHref = $(this).attr('href');
                        urls.push(baseURL + theHref);
                    });
                    var next = $(next_select).first().attr('href');
                    oncomplete(next);
                }
            ]);
    });
}

当前回答

看一看原文:

function socketCloseListener() {
  var socket = this;
  var parser = socket.parser;
  var req = socket._httpMessage;
  debug('HTTP socket close');
  req.emit('close');
  if (req.res && req.res.readable) {
    // Socket closed before we emitted 'end' below.
    req.res.emit('aborted');
    var res = req.res;
    res.on('end', function() {
      res.emit('close');
    });
    res.push(null);
  } else if (!req.res && !req._hadError) {
    // This socket error fired before we started to
    // receive a response. The error needs to
    // fire on the request.
    req.emit('error', createHangUpError());
    req._hadError = true;
  }
}

当服务器从未发送响应时,就会触发该消息。

其他回答

另一个值得一提的情况(对于Linux和OS X)是,如果您使用像https这样的库来执行请求,或者如果您传递https://..。作为本地服务实例的URL,您将使用端口443,这是一个保留的私有端口,您可能会在Socket挂起或ECONNREFUSED错误中结束。

相反,使用端口3000,f.e.,并执行http请求。

我认为“套接字挂起”是一个相当普遍的错误,表明连接已从服务器端终止。换句话说,用于维护客户端和服务器之间连接的套接字已经断开。(虽然我确信上面提到的许多观点对不同的人都有帮助,但我认为这是更普遍的答案。)

In my case, I was sending a request with a payload in excess of 20K. This was rejected by the server. I verified this by removing text and retrying until the request succeeded. After determining the maximum acceptable length, I verified that adding a single character caused the error to manifest. I also confirmed that the client wasn't the issue by sending the same request from a Python app and from Postman. So anyway, I'm confident that, in my case, the length of the payload was my specific problem.

同样,问题的根源是坊间传闻。常见的问题是“服务器说不”。

我使用带有x.509证书身份验证的axios

我的请求是这样的。

 async function callService() {

     const httpsAgent = new https.Agent({
         cert: fs.readFileSync('client.crt'),
         key: fs.readFileSync('client.key'),
         ca: fs.readFileSync('ca.crt'),
     });

     try {
         const result = await axios.post(
             'https://example.com/test/', 
             {"topic": 'home'}, 
             { httpsAgent: httpsAgent }
         );
     }
     catch (error) {
         console.log('error:', error)
     }

}

client.crt 
-----BEGIN CERTIFICATE-----
MIIDWjC....cME70g==
-----END CERTIFICATE-----

client.key
-----BEGIN RSA PRIVATE KEY-----
oBXxnIVby...qvdSsQ==
-----END RSA PRIVATE KEY-----

ca.crt
-----BEGIN CERTIFICATE-----
MIIDQ...8SQvvG5
-----END CERTIFICATE-----

我得到了错误:

Error: socket hang up
at connResetException (internal/errors.js:609:14)
at TLSSocket.socketOnEnd (_http_client.js:458:23)
at TLSSocket.emit (events.js:326:22)

解决方案:

我发现我使用的证书是无效的。: -)

如果您在https连接上遇到此错误,并且它立即发生,则可能是设置SSL连接有问题。

对我来说,它是这个问题https://github.com/nodejs/node/issues/9845但对你来说,它可能是其他东西。如果ssl存在问题,那么您应该能够使用nodejs tls/ssl包重新生成它,只是试图连接到域

我在OCP集群上使用CouchDB时得到了类似的错误。

const cloudantSessionStore = sessionStore.createSessionStore(
  {
    type: 'couchdb',
    host: 'https://' + credentials['host'],
    port: credentials['port'],
    dbName: 'sessions',
    options: {
      auth: {
        username: credentials['username'],
        password: credentials['password']
      },
      cache: false
    }
  }

它应该是“http”,而不是“https”,以连接我的CouchDB实例。希望对遇到类似问题的人有所帮助。