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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With