Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SuperAgent + Node.js Connection Refused

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!

like image 754
JP. Avatar asked Oct 20 '22 16:10

JP.


1 Answers

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.

like image 61
Andy Avatar answered Nov 15 '22 05:11

Andy