Running NodeJS with Load Balancer (Nginx).



NodeJS event-driven Javascript runtime is currently used by many infamous organizations like

  • PayPal.

  • NASA.

  • eBay.

  • Medium.

  • Paytm.

Apart from all the technical advantages, one of the other advantages of NodeJS it is very quick to implement. Just with a few lines of code you can simply spin up a server and start serving easy rest API.

For most of us reaching this article, I expect are already aware of the process involved in spinning up a simple Node JS server. But after taking a couple of interviews I came across the concept that developers are still confused about how can one make their Node JS application scalable. Using the best practices and hands-on coding session will guide you with the exact steps of doing it. The prerequisite of moving along with this tutorial.

  1. Basic understanding of Node JS.

  2. Basic understanding of Nginx.

Even if you are not aware of the Nginx, we will explore it, somewhat in detail. Install NodeJS Latest Stable Version available at the time of you going through this tutorial. Following this link ( Click to reach ) you can download the related version of NodeJS for your related Operating System.


This tutorial will be broadly divided into three major category

  • Firstly we will set up two Node JS servers running on ports 3000 and 3001 on the same system.

  • Secondly, we will define two simple get APIs on both servers.

  • Finally, Nginx will distribute our traffic to each server on a round-robin basis.

Step1: Firstly we will set up two Node JS servers running on ports 3000 and 3001 on the same system.

  1. Create a folder, with an index.js file and add the following line into it. const express = require("express"); const app = express(); app.listen(3000, () => { console.log("Server running on port 3000"); });

  2. npm install express, this will install the express module require to set up a basic API server.

  3. Using the following command you can start the server. node index.js

  4. You will set a message, Server running on port 3000

  5. Similarly, you can start another server, that can run port 3001.

Step 2. Secondly, we will define two simple get APIs on both servers.

  1. Add following line to the both file, i.e. for the server running on 3000 and 3001. On the first server, you can add. app.get("/", (req, res, next) => { res.send("Hi I am 3000 port"); }); On the other server, add similar code, just update the string which we are sending as a response. app.get("/", (req, res, next) => { res.send("Hi I am 3000 port"); });

  2. This will help us in exposing the two Node JS get API services. Restart you both the service

  3. So to access them, just open either http://localhost:3000 or http://localhost:3001 , link on you browser.

  4. You will shown a message on your browser, Either Hi I am 3000 port or Hi I am 3001 port. Based upon the URL you are opening.


Step 3: Finally, Nginx will distribute our traffic to each server on a round-robin basis.

  1. After successfully installing the Nginx and making it run on the server. Use the following link to download the Nginx. ( Click to reach )

  2. Once done you can reach the Nginx conf file based on your operating system. For Mac the location is /usr/local/etc/nginx For Rhel the location is /etc/nginx/ With a simple google search, you can find the location of the nginx.conf file for your operating system.

  3. Now in nginx.conf file, you can update the file with the following configuration. Add inside HTTP tag. upstream api_servers { server localhost:3000; server localhost:3001; }

  4. Moreover, add the location which will help our Nginx server to work as a reverse proxy. Add inside the currently present Nginx server. location / { proxy_buffering off; proxy_pass http://api_servers/; }

  5. Now just check if the file is correct, by the nginx -t. It should return OK.

  6. Once you get OK from the nginx -t command, you can restart the nginx and twice open the URL: http://localhost:80 or http://localhost, in incognito mode and normal browser.

  7. One time you will be getting Hi I am 3000 port on the second hit Hi I am 3001 port. You have to open the link in incognito to get the result, otherwise it will keep showing the same message even after refresh.

Note: Ideally for production we need to run the nginx on 443 with proper certificate, so that it maintain a secure connection. Also, we create a separate configuration file which we import in the main nginx.conf file. But for demonstration purposes, we have updated the basic file. If you willing to learn about nginx.conf for production reach out to me on any media or mail will make a blog and share. Rest the video demonstration of this Article will be uploaded very soon, and you can find link below this.



About the Author Apoorv Tomar is a developer by profession and the man behind Mindroast. You can connect with him on Twitter and also find some good technical content on Youtube.

apoorv-pic.jpg

Hi, thanks for stopping by!

Hope you are getting benefitted out of these articles. Don't forget to subscribe to the Newsletter where we will serve you with the curated content right at your mailbox.

Let the posts
come to you.