Introduction to Node.js install on Cent Os 2021

Learn Introduction to Node.js in 2020, learn how to install and set up node.js now. Node.js is both free and open source, and is used in a variety of applications. Node.js is very powerful to support dynamic content, to name a few.

If you want to get a blog up and running quickly and efficiently, Node.js can simplify the process. Based on JavaScript, most web developers can use Node.js without having to learn a completely new language to perform server-side operations.

Node.js is also very powerful in memory, manages all requests asynchronously, and the package manager included has access to the world’s largest database.

Advantages of Node.js

Node.js has several advantages, such as the following:

Natively supports functions that are asynchronous. For example, when a user requests a Node.js-written script, Node.js remains available for new requests while the current request is being processed.

Supports most Linux distributions and is available for you to use a large number of pre-written packages.

Has most of the basic features that you need built-in. It involves modifying, transferring, and deleting files; as well as working with MySQL, MongoDB, and a host of other things without needing to use the package manager used.

Use the world’s largest package repository: npm.
Different JavaScript syntax script. Node.js is mainly JS on the server side.

Disadvantages of Node.js

Unfortunately, though, Node.js does have it’s share of disadvantages:

Because it’s a relatively new language, it’s often modified codebase compared to something like PHP. It means that in a previous or future version of Node.js, calls or methods you are using may not work.

Node.js can sometimes take longer to write code for, as other libraries can not be easily used. For example, ImageMagick is a common PHP library that is not supported by Node.js.

Exceptions are hard to implement, unlike Java. It means that if you have a JS file that is thousands of lines long, it is almost impossible to find errors in your code.

How to use Node.js


  • Any modern version of Ubuntu, CentOS, or Debian installed. This article will only cover the installation process for CentOS. bUT ITS SIMILAR.
  • A minimum of 256 MB of RAM. Note, this figure depends on the application that you’ll be running.
  • For this tutorial, you’ll also need a text editor, such as Vim or Nano.
  • Can also be installed on windows, but I prefer on ubuntu
  • You need to have knowedge on how to use command line too such as CMD.

Installing Node.js on CentOS.

Update your package manager:

yum update -y

Install node.js

yum install nodejs -y

If you are prompted to import a key, enter Y to continue.

Ensure the installation was successful: Check version installed.

node -v
npm -v

Basic File Type Conventions

All files of Node.js have to end with.js. For example, Quadratic.js can be called a simple quadratic solver. Having said that, as long as the first requirement is met, you can call it whatever you want.

The Node.js API

Typically programming languages have an API, and Node.js is no exception. Check the Node.js docs if you’re lost or need to find the syntax for a function (or method).

NOTE:As mentioned previously, Node.js has a code-base that is updated constantly and as such, functions here may no longer work in later versions.

Creating Your First Node.js Programs

Hello, World!

In this section, we’ll be learning about the most basic program you can create. To begin, head to /~ or /root. Creating your first project is as simple as creating a JS file:

nano HelloWorld.js

Once you are inside your favourite text editor, enter the following:

// For reference, comments are made using '//' added before or after a line. Comments are ignored by the Node.js interpreter.
console.log("Hello, world!"); // console.log() simply outputs text to the terminal.

Exit and save.

Now, launch your program:

node HelloWorld.js

You will see the following output:

[root@test-server ~]# node HelloWorld.js
Hello, world!

Simple Math & Variables in Node.js

In this section, we’ll be learning how to perform basic mathematical operations. To begin, head to your /root directory again and create a file called MathTest.js:

nano MathTest.js

Paste the following code into the file:

var a = 5; // Variables are declared using 'var variableName = value'. The value can be a string, integer, boolean value (ie. true/false) or an object. 
var b = 10;
var c = "Hello, world!";

console.log(c); // This line will output the contents of variable c.
console.log("a = " + a + ", b = " + b); // This line prints out the respective values for a & b.
console.log("a + b = " + (a + b)); // This line prints out the result of (a + b) or (5 + 10). The result should be 15

Save and exit.

When you execute your MathTest.js program, you will see the following:

[root@test-server ~]# node MathTest.js
Hello, world!
a = 5, b = 10
a + b = 15

Starting Our First Webserver in Node.js

In this section, we’ll be learning how to start up a Node.js web server. To begin, create a file called WebTest.js:

nano WebTest.js

Paste the following code:

 // This line includes the HTTP module. Having it included allows us to use it's methods and functions to start a working webserver.
var http = require("http");
var a = 5, b = 10; 

http.createServer(function (request, response) {
    // This will simply output "Request received!" to your terminal when you visit your page.
    console.log("Request received!");

    // This line tells your browser that it should be expecting HTML content to be returned.
    response.writeHead(200, {'Content-Type': 'text/html'}); 

    // The following line adds "Hello, world! a + b = 15" to the body. The <i></i> tags will italicize the text. 
    response.write("<i>Hello, world! a + b = " + (a + b) + "</i>"); 

    // Finally, we'll tell the browser that we're done sending data with 'response.end()' below.

Once you’ve saved the file, run your new program:

[root@test-server ~]# node WebTest.js

Now, visit https://(YOUR_SERVER_IP):8080. or localhost, Make sure to have your firewall configured correctly to allow the request.

You will see Request received! on your terminal and the following in your browser:

Hello, world! a + b = 15

NOTE: In order to close (shut down) WebTest.js, use the following key combination: CTRL + C.

Now that you understand some of the basics, the following section will introduce you to using 3rd party modules, installed via npm.

Installing a 3rd Party Node.js Module and Using It in a Program

In this section, we’ll be extending our first “Hello, world!” program. To begin, we’ll be installing a package called colo. This package allows us to use colours on the terminal.

To begin, we’ll be using npm to install the package:

npm install colo

For reference, you can remove the package with npm remove colo

Once the process completes, you will have access to the colo package. Now, once you’ve opened HelloWorld.js up, add the following line at the top:

var colour = require("colo");

Where you see console.log(...), encapsulate “Hello, world!” with brackets. At the start of the brackets, add

console.log("Hello, world!"));

Your final code will look like the following:

var colour = require("colo");
console.log("Hello, world!"));

Save, exit and run your program. The output will be exactly the same as before, except “Hello, world!” will now be red (and bold) in your terminal.

Node.js Final Remarks

Congratulations for all the simple services being completed. It will give you the information (at least most) to understand the code used in other tutorials. Hopefully, you’re not stopping here — with Node.js there are several more things you could do!.

Want to build more things with node.js? well join me now.

Need a website? contact me now.

2 thoughts on “Introduction to Node.js install on Cent Os 2021”

Leave a Comment

Keith Rainz

Contact me

Along Kafue Road, Chilanga, Lusaka Zambia.

Contact me

Connect with me