Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ECONNREFUSED on running localhost server from NodeJS

I have a NodeJS server up and running on my local machine that is listening to port 50000. From another server, that is also running on my local machine, I need to make a simple GET request to that server, but all I get is an ECONNREFUSED error:

{ Error: connect ECONNREFUSED 127.0.0.1:50000
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 50000 }

My request looks as follows, using request:

var options = {
    url: "http://localhost:50000",
    method: "GET"
}
request(options, function(error, response, body) {
    if (error) {
        console.log("[" + getDateTime() + "] Error connecting to localhost:");
        console.log(error);
        return;
   }
   // continue ...

I know that the server is up and running and that the endpoint is defined, because I can do the request to that exact same url in postman or in my browser and get a response, but somehow not in my NodeJS code.

Anybody have an idea?

like image 701
Guest9875 Avatar asked Jul 10 '26 15:07

Guest9875


1 Answers

Just as commented, changing "localhost" to "http://127.0.0.1:50000" worked for me. I ll leave a link down here for a possible explanation for this issue

https://github.com/node-fetch/node-fetch/issues/1624#issuecomment-1235826631

like image 117
김예군 Avatar answered Jul 12 '26 08:07

김예군



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!