为什么我会收到"Error socket hang up"回复?



我试图抓取一个网站。我正在使用库 https://github.com/request/request,但出现此错误:

Error: socket hang up
    at createHangUpError (http.js:1476:15)
    at Socket.socketOnEnd (http.js:1572:23)
    at Socket.g (events.js:180:16)
    at Socket.emit (events.js:117:20)
    at _stream_readable.js:943:16
    at process._tickDomainCallback (node.js:463:13) +0ms

这是怎么回事?我也尝试重新安装软件包,但我收到同样的错误......感谢帮助。

我最近遇到了这个socket hang up问题。我研究了几天,直到找到适合我的解决方案。所以,也许这会有所帮助。

在创建 http 客户端时添加具有keepAlive: truehttp(s)Agent 属性对我很有用。下面是它可能看起来像的示例:

const http = require('http')
http.request({
        method: 'GET',
        agent: http.Agent({keepAlive:true}),
        host: 'www.google.com',
    })

此属性负责管理客户端 http 连接的套接字。可以在此处找到使用此属性的理由。

我还在堆栈溢出上找到了一个答案,该答案也涵盖了该主题,在这里。

所以,我希望这有所帮助。

检查波纹管链接对于源代码

https://github.com/joyent/node/blob/ba048e72b02e17f0c73e0dcb7d37e76a03327c5a/lib/_http_client.js#L164

或检查以下问题

NodeJS - "套接字挂断"实际上是什么意思?

最新更新