28

I'm making an http request with the node.js client, and I get an ECONNREFUSED error. When I make what appears to be the same request with my browser or curl(1), it works just fine

Here's the node request:

var options = {
  host: 'localhost',
  port: 8080,
  path: '/explorers/1.0/agegroup',
  method: 'GET'
};

var req = http.request(options, function(res) {
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log(e);
});

req.end();

And it gives me the error:

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

But when I make the same request with a different client (curl in this case):

$ curl http://localhost:8080/explorers/1.0/agegroup
{... response JSON ...}

Other notes:

  • I've tried changing the host to www.google.com and the port to 80, and node makes the connection successfully

  • I've tried changing the port of the server, and I can still make requests with all clients but node.js (which still gets ECONNREFUSED)

  • The server to which I'm connecting is the CherryPy WSGI Server. When I try connecting to a node server at localhost:8080, it works fine, which would make me think it's the server's problem, except other clients work with the CherryPy server.

  • I've tried using the same headers that my browser is using, but that doesn't work, and it seems like the problem is at the TCP level anyway, so HTTP headers shouldn't be an issue.

What's wrong with my node request?

aaronstacy
  • 6,189
  • 13
  • 59
  • 72
  • 1
    I am getting this same behaviour - it just started exhibiting, inexplicably. If I figure it out, I'll try to post some info. – Brian M. Hunt Mar 05 '13 at 14:35
  • same for me, i have the problem with the phantomjs webserver, which is supposedly using mongoose. – user1950929 Mar 04 '14 at 23:04
  • After searching for hours, this thread has the answer(s) for my ECONNREFUSED problem with node.js. aaronstacy's and steveorevo's answers are what helped me, which is to replace "localhost" with "127.0.0.1" in the gruntfile. – justinkoh Dec 05 '14 at 16:55

8 Answers8

22

I guess sometimes you just need to step away from the problem...

I found a solution, but it doesn't seem to answer the question, and I don't really like the it.

I changed the CherryPy server configuration to serve at 127.0.0.1 instead of localhost, and the node client started working.

aaronstacy
  • 6,189
  • 13
  • 59
  • 72
  • 1
    interesting ... i have used localhost to connect to redis as well as mysql, but never faced the problem .... were you trying the curl from the same server? (wondering if the hosts file could be having a problem) – Sachin Nayak May 18 '12 at 04:35
  • yes, the curl command and node script were exactly as in the question, both hitting localhost:8080 – aaronstacy May 21 '12 at 18:04
19

I disagree with Soman's disagreement.

I've been pulling my hair out on this one, but yes, sure enough nodejs v0.10.24 and v0.10.25 refuse to connect to a PHP 5.4 development server (PHP's command line server) when invoked as:

php -S localhost:8088

The browser, curl, everything else connects just fine. Yet, sure enough, changing the binding address to be:

php -S 127.0.0.1:8088 

And nodejs' using xmlrpc connects just fine. This occurred on a Mac OS X 10.9.1. Very odd.

Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164
Steveorevo
  • 188
  • 1
  • 4
7

I just faced the same problem.

Note that Windows 7 by default doesn't include an entry for localhost in C:\Windows\System32\drivers\etc\hosts (wikipedia reference)

Adding the entry for localhost has no effect with Node.js (0.10.25) as it apparently skips the hosts file completely.

Use 127.0.0.1 as local address instead, this will works what ever is in your hosts file.

Zachary Dahan
  • 1,419
  • 16
  • 26
Txangel
  • 659
  • 9
  • 10
5

I had the same problem. I was able to resolve it by replacing the localhost or 127.0.0.1 by my system's ip address(use ifconfig for the ip address).

martin552
  • 95
  • 1
  • 11
  • Thank God for Stackoverflow. I wouldn't have thought to try it - but this was the solution. (Neither localhost nor 127.0.0.1 worked for me either) – kris May 28 '20 at 02:24
  • worked like a charm. I spent three days on this stupid issue. – Khizar Iqbal Jul 24 '22 at 17:12
0

$ ifconfig

docker0 Link encap:Ethernet HWaddr ....
inet addr:172.17.0.1 Bcast:172.17.255.255 Mask:255.255.0.0 .....

var dbSettings = {
host: '172.17.0.1',
user: 'root',
password: '.......',
database: '.......',
port: '3306'
};

docker ip -> host

0

what worked best for me was using the same port as my xampp.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

I solve this problem by check my mysqlBench
I found difficult to connect to my local mysql

and I tried:

sudo /usr/local/mysql/support-files/mysql.server start 

It works straight way

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
-1

I exactly faced the same issue where I was accessing a third party url. I tried using dns (subdomain.domain.com) and ip (XXX.XXX.XXX.XXX) both but it did not work.

After some struggle, later I realized that this was a problem at server (subdomain.domain.com) end which was not responsive and gone down.

I disagree this is a problem with localhost / 127.0.0.1 (or domain name vs IP address). ( In above case, it's surely a case of incorrect host mapping)

Soman Dubey
  • 3,798
  • 4
  • 22
  • 32
  • 1
    hm, i don't think i follow -- the dns mapping usually happens in libraries below the node.js/curl level (see gethostbyname(3)). so in the question it wasn't a host mapping issue... – aaronstacy Mar 13 '13 at 14:18