Knowledge Base
MilesWeb / Web Hosting FAQ

Know How to Deploy a Node.js Project on CentOS7

Approx. read time : 3 min

Node.js is basically a server-side platform that’s built on Google Chrome’s Javascript Engine (V8 Engine).

Node.js is widely used for building web-based applications. It basically uses an event-driven, non-blocking I/O model making it highly efficient and lightweight option for running real-time applications that run across the distributed devices.

Here in this article, we will understand the steps to install Node.js using the Node version manager. And then, deploy a Node.js app project on VPS that runs on CentOS7.

Node.js Installation Steps

How to Install Node.js Using the Node Version Manager?

You can even install Node.js through NVM, the Node version manager. This software piece lets you install and maintain the different Node.js versions and the packages associated with them at the same time.

Now, for installing the NVM on CentOS 7, visit: GitHub page .

Go to the README file that’s displayed on the main page. From here, copy the curl or wget command. It will take you to the recent version of the installation script.

Depending on the latest version of NVM, you can download and execute the script by typing the command:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

This will install the nvm script to your user account. To use it, type the command:

source ~/.bashrc

Now, you can ask NVM, which versions it knows, with the command given below:

nvm list-remote

It will list all the versions.

You can easily install a version of Node.js by typing any of the releases you see. Here for example, to get the version nvm install v12.14.1, type:

nvm install v12.14.1

You can even the different versions you have installed by typing:
nvm list

->  v12.14.1

system

You can switch between the versions by typing the command given below:

nvm use v12.14.1

Now at last, to check whether the Node.js version is successfully installed, type the command:

node –version                               

This is how you can install Node.js using the Node.js version manager.

Steps to Deploy a Node.js Project

Step 1: The first step is, create a test project file named app.js

Step 2: Now, go back to /root directory. Enter the command:

cd

Step 3: Then, create the app.js project file.

touch app.js

To modify the app.js project file.

  • Run the below command to open the app.js file

   vim app.js

  • Press the I key for entering into the edit mode and add the content to app.js.

Here is an example, port 3300 is that is occupied by the project. The output is “Hello World.”

You can easily configure the project content and port number on the basis of your requirements.

const http = require(‘http’);

const hostname = ‘127.0.0.1’;

const port = 3300;

const server = http.createServer((req, res) => {

    res.statusCode = 200;

    res.setHeader(‘Content-Type’, ‘text/plain’);

    res.end(‘Hello World\n’);          

});

server.listen(port, hostname, () => {

    console.log(Server running at http://${hostname}:${port}/);

});

Step 4: After adding the above content, press the Esc key to exit the edit mode.

Also, enter: wq command and press the “Enter” key for saving and closing the file.

Step 5: Now to run the Node.js project and get the port number, enter the command given below:

node ~/app.js &              

Step 6: To check the network status and protocol stats:

netstat –tpln | grep :3300

In the example, given above, port 3300 is by default included in the command output. It indicates that the project is running normally.Add port 3300 to TCP_IN on CSF or UFW firewall.

Step 7: Now, in your internet browser, enter http://<IP Address of the Server>:<Port number> in the address bar.

Here in this example, the port number is 3300.

At last, this page will be displayed with “Hello World” text.

Nehal Khatri
Nehal is an ardent content writer. She's passionate about crafting content that's simple but adds value. Her insatiable interest in writing has allowed her to explore her skills. She is adept and can write for different types of content formats.