Host multiple sites using Node.js

This question was asked on Stackoverflow. Several answers which involved using some sort of proxy or reverse proxy in nginx, including this one from Digital Ocean were given. I prefer to do without having to install either Apache or nginx so here is how I do it.

First create a script that would run on startup:

Save this script to /etc/init.d or create a symbolic link in /etc/init.d that points to that script.

$: cd /etc/init.d
$: sudo ln -s ~/startup/ node-apps
$: sudo update-rc.d node-apps defaults
$: sudo update-rc.d node-apps enable

Make it executable

$: chmod +x

Also make sure that you install forever and bouncy

$: npm install -g forever
$: npm install -g bouncy

In the bouncy directory, create a file called routes.json like this

“" : 5000,
“" : 5001

As I commented inline in the startup script above, the iptables command routes all traffic from port 80 to port 8000 where bouncy runs. There are many guides and tutorials on iptables which is a fairly powerful and complicated firewall tool, but for me this one guide seems to make it easy for me to understand. Since this involves port 80, a sudo elevation level is required. bouncy then based on its routes.json mapping, pipes traffic to the right endpoints. So I can easily keep track of all of my Node.js applications, I randomly pick the number 5000 and let this be the starting port where my app1, app2, etc run on.

In the above example, I have and subdomains A records created with my DNS management provider, and they all point to the same IP address of my VPS server.

Lastly, here are a couple of applications that I’m currently working on but prove that the above approach does work:

Driven by passion and patience. Read my shorter posts (possibly duplicated from here but not always)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store