Widada
Widada

Reputation: 595

Why am I getting "Error socket hang up" response?

I tried to scrape a website. And I am using the library https://github.com/request/request, but I get this error:

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

What is wrong here? I also tried to re-install the package, but I get same error... help is appreciated.

Upvotes: 15

Views: 106346

Answers (2)

cazuzaneto
cazuzaneto

Reputation: 205

I recently got this socket hang up problem. I researched for a few days until I found a solution that worked for me. So, maybe this can help.

It worked for me to add the http(s)Agent property with keepAlive: true in creating the http client. Here's an example of what it might look like:

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

This property is responsible for managing the sockets for client-side http connections. The justification for using this property can be found here.

I also found an answer on the stackoverflow that also covers the subject, here.

So, I hope this helps.

Upvotes: 12

Related Questions