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.
Socket hang up error is thrown in two cases. When you are a client & When you are a server/proxy When you, as a client, send a request to a remote server, and receive no timely response. Your socket is ended which thrown this error. You should catch this error and decide how to handle it: whether retry the request, queue it for later, etc
Few of them are: • Malicious software, spyware or virus • Windows registryproblems Windows socket errors are going to take place frequently unless you clean your computer regularly. How to fix windows socket error? Try the following two solutions: Solution#1
The server I'm connecting to is not sending a Content-Length header in the response, in which case according to the HTTP spec the server should close the stream after sending all data. I suspect this is being misinterpreted as a socket hang up. Making the same request with cURL works fine. Sorry, something went wrong. Sorry, something went wrong.
Just like an adapter which connects two things that directly cannot be connected, Windows socket plays a role of connecting bridge between software and network. If it gets corrupt or doesn’t work, your windows software might not be able to access internet.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With