top of page
Search

Node.js - PC To Production

Writer's picture: Sachin TahSachin Tah


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

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


Running Node.js Program


Running Node.js applications are fairly simple, just go to the 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 the entire application eventually bringing a halt to entire traffic if configured on only a single server. Once crashed, you need to restart your application again using the same command line mentioned above.


Of 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 the 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 the 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 easier.

Node on Production

To run Node.js on a production server, first, you need to install a stable Node.js version on the 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 the 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 anyway, 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.


What's a Reverse Proxy?


Most of the advanced web servers come with a built in feature called a reverse proxy. Apache, Nginx provides one such feature. A reverse proxy works exactly the opposite of a proxy server, it reroutes your request to some other location and serves you the contents received from that location.


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




SSL certificate can also be installed on a reverse proxy which will take away SSL implementation pain at Node.js level. A 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 set up a load balancer, below architecture diagram describe how you can configure a load-balanced 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, the 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

14 views0 comments

Recent Posts

See All

Comments


bottom of page