Reputation: 5594
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!
Upvotes: 5
Views: 3091
Reputation: 17781
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:
node
rather than leaving the firewall disabled)sudo setenforce 0
if you're on a *nix (undo with sudo setenforce 1
)user-agent
strings - do you have a proxy running? Run curl http://localhost:5000/ -vvv
to see exactly what curl is doingIf 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.
Upvotes: 3