DEV Community

jsstackacademy
jsstackacademy

Posted on

Deploy NodeJS application using Nginx

Image description

Hosting a NodeJS/ExpressJS application using Nginx

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine.

NodeJS applications can be hosted in many different ways but the most preferred way is using Nginx reverse proxy server. Following this article you will be able to host your own NodeJS + Express application for free on any cloud provide if you have Free credits available ( Which mostly have if you are creating your account for the first time).

For this replication you will need a account on any cloud provider. I will be using AWS.

Let's get started:

Step 1: Setting up cloud environment 👽:

Goto your cloud console and create a Virtual machine. The below commands I have shared are using Ubuntu 18.x on Amazon EC2. But Feel free to choose any machine and install the below mentioned packages accordingly.

Step 2: Installing and setting up NodeJS:

As mentioned above NodeJS is an open-sources. Hence, it is available for free for any different machine. To install NodeJS in Ubuntu follow:

Check for latest updates:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Now, Install Node.js:

sudo apt install nodejs
Enter fullscreen mode Exit fullscreen mode

Check if Node.js is successfully installed and the version:

node --version
Enter fullscreen mode Exit fullscreen mode

Now, Install node package manager

sudo apt install npm
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a simple Node & Express application

The below mentioned code will serve Hello World if anyone visit your webpage:

Initialize your Nodejs application:

cd ~
mkdir mynodeapp && cd mynodeapp
npm init -y
Enter fullscreen mode Exit fullscreen mode

Install Express

npm i express
Enter fullscreen mode Exit fullscreen mode

Copy the following code into your app.js file:

vim app.js
Enter fullscreen mode Exit fullscreen mode
const express = require("express"); 
const app = express(); // Initializing Express App

// Sending Hello World when anyone browse your webpage

app.get("/*", (req, res)=>{
        res.send('Hello World'); 
});

app.listen(3000, ()=> console.log("App Listening on port 3000"));
Enter fullscreen mode Exit fullscreen mode

Save the above code and exit by pressing esc button with :wq

Test your application:

node app.js
Enter fullscreen mode Exit fullscreen mode

Expected Output:

App Listening on port 3000

To close the above application press Ctrl-C


We can see that if we run the above application, it blocks all other commands and we are not able to perform any other activities. Further, if we will close the terminal the app will be closed. To fix this issue we will be using pm2, this will help us to run multiple NodeJS/ExpressJS applications on different ports at single time without blocking the resources or us

Installing PM2

sudo npm install -g pm2
Enter fullscreen mode Exit fullscreen mode

Start the above application using pm2:

pm2 start app.js
Enter fullscreen mode Exit fullscreen mode

Check if you application is running or not:

pm2 status
Enter fullscreen mode Exit fullscreen mode

To stop your application you can do:

pm2 stop <application_name/ID>
Enter fullscreen mode Exit fullscreen mode

Your application name for us it is app.js or ID will be 0 as shown above in Output of pm2 status

To restart your application:

pm2 restart <application_name/ID>
Enter fullscreen mode Exit fullscreen mode

--

After starting your application using pm2; Test your application using CURL if it is running successfully running locally or not

curl localhost:3000
Enter fullscreen mode Exit fullscreen mode

Step 3: Setting up Nginx Proxy

We can see that our application is currently running on port 3000. However, we want whenever anyone comes to our application they just directly open our website let say example.com and they should be redirected to our application. To do so, we will use Nginx which redirect users coming on our website to our application which is running on port 3000. Using nginx we can setup multiple different websites quickly and store/manage logs easily.

For Ubuntu, Nginx is available in it's default repositories, hence we simply need to install nginx directly

sudo apt install nginx
Enter fullscreen mode Exit fullscreen mode

In-order to setup Nginx open defult nginx file and setup a reverse proxy to localhost:3000

sudo vim  /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

In the default file, under the server block you can see pre-existing location block. Remove the content of location / with the below mentioned code:

 location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
Enter fullscreen mode Exit fullscreen mode

Start your nginx server

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

wuhoo!! 🎉 we successfully hosted your application which is now running on:

http://your_virtual_machine_IP
Enter fullscreen mode Exit fullscreen mode

Add your_virtual_machine_IP to your domain name provider as ** A name** and anyone visiting your website will be able to see your NodeJS application.

For, daily content follow me on Instagram @mr_javascript


Refer to the below link for Step by step video tutorial

Youtube video by JSStackAcademy

Top comments (0)