Node Multi site hosting with Forever & Bouncy

Having hosted web applications on IIS for years and then trying out PaaS solutions like Heroku, I thought that it would save me money in the long run to have my own server and would be fun trying to get it work!

There are many solutions out there for IaaS (Infrastructure as a Service) and I wont get into the reason for selecting Linode but that's what I opted for.

I also used the git solution for deployment to the server by committing changes to git and then cloning the repo when on the server. Once that was done (and node was installed) I used NPM to install the dependencies.

This was working great. Using nodemon to check that my site was working is a doddle.

Now since I've got a new shinny server I'll be wanting to get my money's worth so I'll get another site on there. Using git I've now got two sites on the server but in all honesty I've only ever started a node server with npm, grunt, or nodemon. How do you do start a node server and keep it running when the terminal is closed?

The answer i found was Forever.

To install npm install -g forever and now i can use forever to start my node servers and keep them running for me.

cd <AppServerFolder>
forever start app.js

The response in the terminal will be

warn:    --minUptime not set. Defaulting to: 1000ms
warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info:    Forever processing file: app.js

and if you want to see the number of processes that forever is running then forever list

info:    Forever processes running
data:        uid  command         script   forever pid  id logfile                          uptime        
data:    [0] hcD3 /usr/bin/nodejs index.js 7080    7081    /home/xxxx/.forever/hcD3.log 0:1:56:58.193 
data:    [1] aqsG /usr/bin/nodejs app.js   8343    8344    /home/xxxx/.forever/aqsG.log 0:0:1:20.775  

You'll notice that each process has its own log which can be opened with:

cd .forever
forever ls <- to see the files
nano hcD3.log

So this is great. I've got two sites running on my server and they are using different ports but if I have two seperate url's how do I route the user to the correct one?

The answer to this question was bouncy.

Bouncy will be acting as the director of traffic, so if a request comes in to my server on port 80 (the port that bouncy will be listening) then it'll then check the request url and send the request to the correct port.

Firstly to install bouncy: npm install -g bouncy.

Now there are two ways in which you can have this running. The first was is to have a json file which bouncy will reference when its handling traffic.

The json will look like this:

{
    "site1URL": 8001,
    "site2URL": 8002
}

I'll save this as route.json and use it with bouncy route.json 80. Here I'm telling bouncy to use the route.json file and to listen on port 80. So if a request comes to the server for site1URL then bouncy will redirect the traffic to port 8001.

This is all very well and good but won't work if you want to close the terminal. We'll have to have this running with forever some how.

The second way is to create a js file (I've called mine server.js) that will require bouncy and create a server for us.

var bouncy = require('bouncy');

var server = bouncy(function (req, res, bounce) {
  if (req.headers.host === 'site1URL') {
      bounce(8001);
  }
  else if (req.headers.host === 'site2URL') {
      bounce(8002);
  }
  else {
      res.statusCode = 404;
      res.end('no such host');
  }
});
server.listen(80);

and to use this forever start server.js which will keep the process running in the background.