1

I have written some code using modules cheerio, request and longjohn that worked yesterday, but today it always throws the "ECONNREFUSED" error.

I tried using simple sample code that uses request:

var request = require('request');
request('http://www.google.com', function (error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body) // Show the HTML for the Google homepage.
    }
    else{
        console.log(error);
    }
})

Yet it still returns:

{[Error: connect ECONNREFUSED]
    code: 'ECONNREFUSED',
    errno: 'ECONNREFUSED',
    syscall: 'connect' }

Just wondering what could be the cause of this, I can connect to the internet fine using web browsers.

Thanks

ruby
  • 13
  • 2

2 Answers2

0

That is probably because the process can not have access to the internet because of a firewall or proxy.

Check your firewall and proxy settings and make sure the process have access to internet.

Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52
0

My problem was I was performing a get immediately after starting a server process. I solved the error by wrapping the get in a setTimeout

setTimeout(() => {
  http.get('http://localhost:4003/state', (response) => {
    console.log('response:', response)
    response = response;
    server.kill('SIGTERM');
  }).on('error', (err) => {
    console.log('error:', err)
    server.kill('SIGTERM');
  });
}, 1000);
Harry Moreno
  • 10,231
  • 7
  • 64
  • 116