Reposting from https://medium.com/@kevinle/host-multiple-sites-using-node-js-fff1ef7d0534.

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:

#!/bin/bash
#
# An init.d script for running a Node.js process as a service using iptables, forever and bouncy
# Reference:
# 1- https://github.com/nodejitsu/forever
# 2- https://www.exratione.com/2013/02/nodejs-and-forever-as-a-service-simple-upstart-and-init-scripts-for-ubuntu/

source /home/kle/.nvm/nvm.sh
NAME="Script for all NodeJS apps"
NVM_VERSION="v0.10.31"
NVM_CURRENT=$(nvm current)
####NVM_VERSION=$(grep -o "v.*$" <<<"$NVM_CURRENT")

start() {
echo "Starting $NAME"

##### First, route all traffic to 80 to 8000 which is where bouncy runs
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000

##### Second, run bouncy at port 8000 which routes traffic according to routes.json in bouncy directory
$NVM_DIR/$NVM_VERSION/bin/forever start --spinSleepTime 10000 $NVM_DIR/$NVM_VERSION/bin/bouncy /home/kle/projects/bouncy/routes.json 8000

##### Run application1 on port 5000
cd /home/kle/projects/app1
$NVM_DIR/$NVM_VERSION/bin/forever start server.js

##### Run application2 on port 5001
cd /home/kle/projects/app2
$NVM_DIR/$NVM_VERSION/bin/forever start server.js

##### Set current directory back to ~
cd ~
RETVAL=$?
}

stop() {
echo "Shutting down $NAME"

##### Undo line 18 above
sudo iptables -t nat -D PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000

##### stop all forever processes
$NVM_DIR/$NVM_VERSION/bin/forever stopall

RETVAL=$?
}

restart() {
stop
start
}

status() {
echo "$NAME is running."
$NVM_DIR/$NVM_VERSION/bin/forever list
RETVAL=$?
}

case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo "Usage: {start|stop|status|restart}"
exit 1
;;
esac
exit $RETVAL

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-startup.sh node-apps
$: sudo update-rc.d node-apps defaults
$: sudo update-rc.d node-apps enable

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

{
“app1.domain.com" : 5000,
“app2.domain.com" : 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 app1.domain.com and app2.domain.com 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:

http://react-mui-auth.codeprototype.com/

http://connect5.codeprototype.com/