Question

Is it possible to run a Node.js application that is accessible to the internet without a reverse proxy?

Posted August 16, 2017 1.5k views
Node.jsUbuntu 16.04

Is it possible to run a Node.js application that is accessible to the internet without a reverse proxy?

If so how does one set it up? IP and port wise.

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.

×
1 answer

You can actually do this with iptables pretty easily:

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000

You’ll want to add that line (minus sudo) to your /etc/rc.local file as well to start the redirection on boot. This also assumes you’re running your app on port 3000 (configured in your code), but you can redirect it to wherever by changing the final value in that command.

Alternatively, if you’re using Express, the app.listen call accepts a hostname or IP address:

app.listen(80, <the IP address you want to listen on>, function() {<your callback here>});

I’ve heard a lot of people say not to run apps directly on port 80, and although I can’t think of a reason off the top of my head, instinctively it seems like a bad idea. Any particular reason you don’t want to use a reverse proxy?

Submit an Answer