Updating a Node.js web application without downtime

I have a Node.js/Express web application running on localhost:3010 that's on, say, version 1.2.2. Suppose I want to update the web application to version 1.3.0 without any downtime. What's the best way?

Using NGINX in front of the Node.js web application

Fortunately NGINX sits in front of my web application. Incoming HTTP requests are sent first to NGINX, and then NGINX forwards the requests to my Node.js/Express web application. Here's what my configuration file looks like from /etc/nginx/sites-available/www.arepeopletalkingaboutit.com

There are two things worth noting. The root directory and the port. In this situation, my Node.js application is hanging out at /home/arp/apps/arepeopletalkingaboutit-3010/ and is being served on port 3010. In my Node.js/Express application's bin/www I have:

var port = normalizePort(process.env.PORT || '3010');

Spin up a second Node.js web application

I'll actually start up a second Node.js web application with the new version in a new directory and on a new port. Your process may look different than mine, but mine looked something like this:

At this point, both Node.js/Express web applications are running, but only one is reachable from the world wide web due to our configuration of NGINX:

A hot reload of NGINX

NGINX has a neat feature where you can reload its settings without any downtime. This means we can change the settings in /etc/nginx/sites-available/www.arepeopletalkingaboutit.com and save the file, but the changes won't go into effect until we reload or restart NGINX.

Verifying the changes

I always like to do a sanity check to make sure this is working. The easiest is to make sure I can still go to the web application at www.arepeopletalkingaboutit.com. I'll keep both logs open by using the tail command and see which web application handles the request.

I really like forever to keep my Node.js / Express web applications alive and running, so check that out if you're curious.