5

I'm pretty new to Node.js so this is possibly an basic understanding issue, but I'm getting ECONNREFUSED from a superagent http request when I don't think I should be:

$ curl http://localhost:5000/
{"you": "looking good"}

$ node
> var request = require("superagent");
> var req = request.get("http://localhost:5000/").on('error', function (err) {
    console.log("There's an emergency is going on.", err)
  }).end(function (data) {
    console.log("All is well", data.body)
  });

There's an emergency is going on. { [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect' }

What assumption have I made that is breaking this? I'm actually writing isomorphic JavaScript and the exact same code making the http query on the browser works fine!

JP.
  • 5,507
  • 15
  • 59
  • 100

1 Answers1

3

This sounds like a firewall problem, but first run ping localhost from a terminal. You should see localhost has IP 127.0.0.1. If it doesn't your hosts file is probably incorrect or not being loaded, and will need fixing.

If it's correct, try these one at a time then repeat the curl and superagent requests for comparison. If one step doesn't work, reverse it and move to the next:

  1. disable any firewall (if this is the problem, you should add a rule for node rather than leaving the firewall disabled)
  2. disable any anti-virus (again add permissions rather than permanently disabling)
  3. sudo setenforce 0 if you're on a *nix (undo with sudo setenforce 1)
  4. curl and superagent will use different user-agent strings - do you have a proxy running? Run curl http://localhost:5000/ -vvv to see exactly what curl is doing

If these still don't work you could try a packet capture tool like wireshark or tcpdump to trace the HTTP request and see where it's refused.

Andy
  • 17,423
  • 9
  • 52
  • 69
  • Thanks Andy, I still can't figure out exactly why, but running on `0.0.0.0` and accessing through my external IP rather than loopback fixed it. It's not a firewall issue (Mac OS; all firewalls down), or an anti-virus thing, there is no SELinux and the user agent doesn't seem to have any effect. I'll put it down to the magic gnomes. Thanks again. – JP. Feb 25 '15 at 11:47