Skip to content

How to Fix localhost Connection Refused

DodaTech 1 min read

In this tutorial, you'll learn about How to Fix localhost Connection Refused. We cover key concepts, practical examples, and best practices.

The Problem

You try to connect to a service on localhost and get:

curl: (7) Failed to connect to localhost port 3000: Connection refused

Or:

Error: connect ECONNREFUSED 127.0.0.1:3000

The service you are trying to reach is not running, is listening on a different port, or is bound to a different network interface.

Quick Fix

Step 1: Check if the service is running

ps aux | grep node
# or
ps aux | grep python
# or check your specific service

If the process is not listed, start the service.

Step 2: Find what is listening on the port

sudo ss -tlnp | grep 3000
# or
sudo lsof -i :3000

If nothing appears, no process is listening on port 3000.

Step 3: Check if the service binds to the right interface

Your service might be bound to 0.0.0.0 or a specific IP. Check the service configuration:

# For a Node.js app
grep "listen" server.js
# For Nginx
sudo nginx -T | grep listen

Step 4: Start the service

# If it's a Node.js app
node server.js

# If it's a systemd service
sudo systemctl start my-service
sudo systemctl status my-service

Expected:

Active: active (running)

Step 5: Test again

curl http://localhost:3000

Expected:

<!DOCTYPE html>...

Alternative Solutions

If another process is on the port, use a different port:

# Run on port 3001 instead
PORT=3001 node server.js
curl http://localhost:3001

Prevention

  • Always check if the service is running before debugging connection issues.
  • Use systemctl to manage services so they start automatically.
  • Log the port the service starts on to make debugging easier.
  • Use a process manager like pm2 for Node.js or gunicorn for Python.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro