13

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.

ford04
  • 66,267
  • 20
  • 199
  • 171
Widada
  • 575
  • 2
  • 4
  • 17
  • Possible duplicate of [NodeJS - What does "socket hang up" actually mean?](https://stackoverflow.com/questions/16995184/nodejs-what-does-socket-hang-up-actually-mean) – OrangeDog Aug 01 '18 at 10:30

2 Answers2

10

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.

cazuzaneto
  • 175
  • 2
  • 9
1

check the bellow link for the source code

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

or check the following question

NodeJS - What does "socket hang up" actually mean?

Community
  • 1
  • 1
hussam
  • 596
  • 5
  • 12