Question

How can I get Node.js to listen on port 80?

Hi! I followed the instructions here, https://www.digitalocean.com/community/articles/how-to-install-node-js-with-nvm-node-version-manager-on-a-vps, and I got a Node app up and running on port 3000. When I direct requests to that port, I see my app, but not when I send a request without specifying a port.

I tried running $ PORT=80 node app, and Node threw an error and quit. It worked when I used PORT=8080 though.

On IRC, @kamal_ suggested I install and configure NGINX to to serve the static files “and pass everything else to Node.js”, and now I have NGINX up and running. When I go to my droplet’s IP now, I see “Welcome to nginx!” instead of having the connection refused, but I still have to append :3000 to the URL to see my Node app.

This is the NGINX config file I got from @kamal_: https://p.kk7.me/mepayofuqo.nginx

What are the next steps to making the app load on requests to the default port?

Thanks in advance!


Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

For people looking for a quick and easy solution, not the most robust, but it will get you running, try: <br> <br>redirect port 80 to port 3000 with this command: <br> <br> sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000 <br> <br>Then launch Node.js on port 3000. Requests to port 80 will get mapped to port 3000. <br> <br>You should also edit your /etc/rc.d/rc.local file and add that line minus the sudo. That will add the redirect when the machine boots up. <br> <br>found this (here)[http://stackoverflow.com/questions/16573668/best-practices-when-running-node-js-with-port-80-ubuntu-linode] Hope this helps someone.

You can use the proxy

   server_name  example.com;
    access_log /var/log/nginx/your.log;
    location / {
            proxy_pass http://127.0.0.1:3000/;
    }

Launching the app with sudo works fine. There’s probably security implications. I’m not a backend guy. But it will work for now.

Edit: No, it’s not… I give up.