Why am I getting “Error socket hang up” response?

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.

Leave a Comment