Report this

What is the reason for this report?

How to connect to running nodejs process?

Posted on December 25, 2024

First time setting up a droplet with digital ocean. I have an ubuntu slice running. I can ssh and sftp. I have nodejs installed and running a server process on port 8000. I can’t seem to hit it from the browser. Any ideas?

sudo ufw status Status:inactive

no firewall running according to my DO management console

please help



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

Verify the Node.js Server is Running:

Ensure your Node.js server is up and listening on the correct port:

  1. Run the following command to check if the server is listening on port 8000:
sudo netstat -tuln | grep 8000

You should see something like:

tcp        0      0 0.0.0.0:8000            0.0.0.0:*              LISTEN

Test locally from the droplet:

curl http://localhost:8000

If you get a response, the server is running correctly.

  1. Check Node.js Binding

Your Node.js server may be bound to localhost (127.0.0.1) instead of 0.0.0.0, meaning it’s only accessible locally.

  • Open your Node.js server file and look for the line where the server listens, e.g.:
app.listen(8000, 'localhost');

Change 'localhost' to '0.0.0.0' Restart the Node.js process

Hey there!

If you want to access the Node.js app directly on the port itself, you need to make sure your Node.js process is listening on 0.0.0.0 and not just localhost (127.0.0.1). If it’s bound to localhost, it won’t be accessible externally.

Open your Node.js app and check the server code. It should look something like this:

const http = require('http');
const server = http.createServer((req, res) => {
    res.end('Hello, world!');
});

server.listen(8000, '0.0.0.0', () => {
    console.log('Server is running on port 8000');
});

If it’s using 127.0.0.1 or not specifying the host, update it to 0.0.0.0.

To verify if this is the case run this command to check if your Node.js app is running and listening on port 8000:

sudo netstat -tuln | grep 8000

You should see something like this:

tcp   0   0 0.0.0.0:8000   0.0.0.0:*   LISTEN

If it’s not running, double-check your app and logs to ensure it’s starting correctly.

For a robust and secure setup, follow this guide from DigitalOcean instead: 👉 How To Set Up a Node.js Application for Production on Ubuntu 20.04

This guide will walk you through:

  • Setting up your Node.js app using pm2 to keep it running in the background.
  • Using Nginx as a reverse proxy to forward traffic from port 80 to your Node.js app.
  • Configuring firewall rules securely.

Here’s a quick example of an Nginx config to proxy your Node.js app:

server {
    listen 80;
    server_name your_domain_or_ip;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

After setting this up, restart Nginx:

sudo systemctl restart nginx

Let me know how it goes!

- Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.