Question

Can't reach my Node.js application

Posted April 18, 2019 18k views
Node.jsUbuntu 18.04

Hello world!
I’ve created a Ubuntu NodeJS 8.10.0 on 18.04 droplet.
I’ve deployed a Node.js app, tested on my computer.
I can reach my app from inside the Droplet.
curl http://localhost:3000
I can’t reach my app from my computer.
curl http://123.456.789.012:3000
This is not the real IP.

Any ideas?

Thank you!!!

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.

×
2 answers

Greetings!

I would bet that your application is listening on 127.0.0.1, as opposed to the public interface. If you run netstat -tulpn you should see the applications listening, and it might look something like this:

[root@gateway] ~ # netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      693/systemd-resolve
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1392/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      1392/sshd
udp        0      0 127.0.0.53:53           0.0.0.0:*                           693/systemd-resolve
udp        0      0 10.128.0.5:68           0.0.0.0:*                           673/systemd-network
udp        0      0 127.0.0.1:323           0.0.0.0:*                           1756/chronyd
udp6       0      0 ::1:323                 :::*                                1756/chronyd

Notice on my system that systemd-resolve is listening on 127.0.0.1, so it is only reachable internally. Meanwhile, sshd is listening on 0.0.0.0 which means all interfaces, making it accessible both internally and externally.

Your application most likely defines this somewhere in it’s configuration, and can be adjusted. If you’re not sure where, maybe a grep through it’s code can help, something like:

grep -R "127.0.0.1" *

Or even:

grep -R "localhost" *

If it isn’t configured anywhere, it’s probably an assumed default and it is required that you specifically define the value to change it.

Jarland

  • Thank you Jarland! I really appreciate your answer.

    This is what I get when I run netstat -tulpn.

    tcp6       0      0 :::3000                 :::*                    LISTEN      4488/node 
    

    I’m sure that this configuration is not on my code. Actually, this is my code.

    const connect = require('connect');
    const http = require('http');
    
    const app = connect();
    
    app.use(function(req, res) {
      res.setHeader('Content-Type', 'text/plain');
      res.end('hello world!');
    });
    
    http.createServer(app).listen(3000);
    

    I’m really confused. Where is the point to have a Node.js droplet if I can’t access to it from outside? Shouldn’t it be the deault behavior?

    • I think that’s listening on all interfaces so you should be good. To be honest not everyone exposes their nodejs applications directly to public interfaces, a lot of people reverse proxy them with Nginx so that you can have a proper web server with caching between the visitor and the nodejs app, as an intentional web server tends to handle traffic better.

      I’d say it’s listening properly, so two things I’d look at next:

      1. Is port 3000 open in the droplet’s firewall? Try this to open it: iptables -I INPUT -p tcp --dport 3000 -j ACCEPT

      2. Is there anything on your computer or network that would block unusual ports that you try to access remotely? Internet security software, Windows firewall, a locked down corporate network, any of these could be responsible for blocking anything other than standard ports like 80, 443.

iptables -I INPUT -p tcp –dport 3000 -j ACCEPT

worked for me, thanks in milllions

Submit an Answer