Bb496d7d80e81ca1c0f522ab74eb674a35689ee5
By:
basickarl

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

August 16, 2017 300 views
Node.js Ubuntu 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.

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?

Have another answer? Share your knowledge.