Node.js - PC To Production

Node.js - PC To Production

Applications developed targeting Node.js are platform independent and works on almost on all OS like a charm. Same goes with deployment, it is very easy to deploy Node.js application to a production be it cloud or a traditional environment.

I am deploying Node.js on production servers of our customers with virtually zero downtime, yes thats possible without much of a hassle, so your live traffic is not effected while you are performing a silent release.

Running Node.js Program

Running Node.js applications are fairly simple, just go to command prompt and type

mymac:~admin$ node app.js

Unlike ASP or JSP, Node.js web applications are hosted within its own web server along with the fact that it runs on a single thread. This simply means that any unhandled exception will crash entire application eventually bringing halt to entire traffic if configured on only a single server. Once crashed, you need to restart your application again using same command line mentioned above.

Off course, we can handle errors gracefully so that it won't crash however there is always a possibility. There is a simple way to automate restart of your node application, you need to use ready made available Node.js library "Forever".

Installing Forever

mymac:~admin$npm install -g forever  

-g argument will install forever globally and will be available to all node applications on the pc.

Now instead of running your application using node command, you need to run it via forever

mymac:~admin$forever start app.js  

Pretty simple!!! Now every time your application crashes, forever will create a new instance of your application automatically and add crash information in forever logs. Some more advanced tools like Supervisord are also available to make life more easy.

Node on Production

To run Node.js on a production server, first you need to install a stable Node.js version on server, secondly you need to clone your app from github or any other repository.

You can run your Node.js web application on production boxes directly without any layer in between as shown in the picture below, simply copying Node.js application directory structure on your server and run the application using forever. You may need to configure firewalls to allow access of your port from outside.

The above implementation works but is not advisable anyways, as your Node.js application is directly exposed to the outside world. There are also other disadvantages like SSL implementation, port 80 unavailability, firewall configurations etc, so better stay away from this implementation as much as possible.

Whats a Reverse Proxy?

Most of the advance web servers comes with a built in feature called reverse proxy. Apache, nginx provides such feature.

A reverse proxy works exactly opposite of a proxy server, it reroutes your request to some other location and serve you the contents received from that location.

You can use Node.js in conjuction with a reverse proxy as shown below

SSL certificate can also be installed on reverse proxy which will take away SSL implementation pain at Node.js level. Reverse proxy can also compress contents and along with serving static contents. To download and configure nginx, please visit below URL http://nginx.org/en/docs/beginners_guide.html

Load Balancing

To scale up your Node.js application horizontally, you can use a load balancer. There are different ways you can setup a load balancer, below architecture diagram describe how you can configure a load balance environment for your Node.js application using HA Proxy

If you are deploying Node.js on a cloud platform like AWS or Azure, you can directly use off the shelf Enterprise Load Balancer (ELB) on AWS or Azure Load Balancer on Azure environment and configure it point to your servers.

As per my personal experience, there are some advantages of using a HA Proxy over ELB or Azure Load Balancer, reason I will discuss this in some other post.

HAProxy can also act as a single point of failure, to eliminate this you can use routing application keepalived available on Linux platforms.

For more information on keepalived, please visit www.keepalived.org

Sachin Tah







Nice post sachin sir! It clears few of my doubts around node.js. Waiting for more posts like this!

To view or add a comment, sign in

More articles by Sachin Tah

Others also viewed

Explore content categories