Guest9875
Guest9875

Reputation: 93

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?

Upvotes: 7

Views: 21311

Answers (6)

pragmateek
pragmateek

Reputation: 936

I'm using bun instead of node here. Recently I had to shutdown my mac because multiple heavy processes running had slowed it down. After booting up, I tried to start the server using:

bun index.js

as I always do. That is when started getting this error:

{"code":"ECONNREFUSED","syscall":"connect","errno":1}

while the command tried to start any process. Simply googling the result didn't help much. Thankfully a friend knew what was happening and said that redis is not running in background which causes this.

So before running the project I simply ran:

brew services start redis

after that bun index.js was able to start the server locally.

Conclusion:

Ensure if any process on which your project environment depends on are still running. It could be redis (in my case) or a DB connection like mongodb or postgresql service

Upvotes: 1

mathems32
mathems32

Reputation: 355

In my experience, Nodejs isn't so specific with what's actually causing an error. I received this ECONNREFUSED error message in the context of Firebase Cloud Messaging, trying to send a message. After reading Stack I thought it meant I should setup a proxy situation, but that made no difference bc my connection wasn't really the problem. It was an error in the object I drafted to send which needed editing - after editing the error went away and everything was fine. Bear this in mind: consider how one wrong thing can make another appear to be wrong.

Upvotes: 0

Shubham Arora
Shubham Arora

Reputation: 947

Use this at the beginning of your code:

const dns = require('dns');

// Set default result order for DNS resolution
dns.setDefaultResultOrder('ipv4first');

Upvotes: 8

Mohammed Amir Ansari
Mohammed Amir Ansari

Reputation: 2401

The possible issue is that some else process is already running on the same port you are trying to use, either change your port or kill the existing process on your port. To kill the process on port you can try:

For mac:

sudo kill $(lsof -t -i:8000) 
# or 
sudo fuser -k -n tcp 8000 
# or 
fuser -k 8000/tcp

And for windows check this

hope this helps :)

Upvotes: 3

김예군
김예군

Reputation: 349

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

Upvotes: 14

sid
sid

Reputation: 363

You might be running both the servers on the same port, kill another server on same port.

If you're on linux, you can kill port using sudo fuser -k -n tcp 5000

or if you're using windows: taskkill /PID 5000 /F

Upvotes: 2

Related Questions