X
22Jan

An introduction about Node.JS and usage of its modules

Node.js is an open source, cross platform JavaScript runtime environment for developing variety of tools and applications. Node.js is a platform that built on Chrome’s JavaScript runtime. Node.js applications will be developed in JavaScript and they run within Node.js runtime.

In this article I am going to show you how to setup Node.js, how you can use it after setup, usage of Node.js modules, NPM packages and Node.js HTTP Server.

Installation and Verification

Firstly, download the Node.js from the below link and install it like you do for any other software.

https://nodejs.org/en/download/

By default Node.js uses C:\Program Files\nodejs as the default installation path. At the time of installation Node.js will set this directory path in system environment variables. In case if you chose any other path, make sure it is properly registered in environment variables.

nodejs

Once you install the Node.js, you must verify the installation to make sure Node.js installed as expected. You can verify the Node.js installation by using one of the following methods.

Method 1: search for Node.js from your windows search and find the Node.js file highlighted in below picture.

nodejs

Select the Node.js file to launch its Node.js shell as shown below.

nodejs

Just provide your JavaScript statement as I did in above picture. If you can see the output properly, your Node.js installation is proper and start using it.

If you want to exit the Node.js, give Node command .exit and hit enter.

Method 2: You can also verify the Node.js installation from your regular command prompt by giving command node as shown below. Once you given node command, you will be switched to Node.js shell.

nodejs

Once you switched to Node.js shell, give your JavaScript statement which is done in Method 1. If you can see the output of your JavaScript statement, then your Node.js setup is proper.

If you want to switch back to your regular command prompt, type .exit command and hit enter

Run JavaScript in Node.js

Now we will start use Node.js runtime to execute the JavaScript. For this open a notepad (or any other JavaScript editor), write JavaScript code as shown in below example and save it as “sample.js” in E Drive.

nodejs

Now open the command prompt, navigate to E Drive and use command node sample.js as shown below.  The JavaScript code which is in “sample.js” will be interpret and executed by Node.js runtime and shows the output in command prompt.

nodejs

Node.js Modules

Node.js has a module loading feature where one JS file can be loaded into another JS file. Which is a very useful feature where if you have a dependent files.

To use the module features, create one more JavaScript file, name it “module1.js” in the same location where “sample.js” is available. Once you done with adding your code to “module1.js” file, go to “sample.js” file and use require statement as shown below to load “module1.js”

nodejs

Below is the output when you run the above “sample.js” file in Node.js.

nodejs

Now let’s make the “module1.js” little bigger with couple of methods to call them from “sample.js” as shown below:

nodejs

As you can see in the above “sample.js” JavaScript code, dependent “module1.js” file is loaded on top and making the calls to module1 at the bottom of the code. Below one is the output when you execute the above “sample.js” file.

nodejs

That’s it about loading modules. Isn’t it so simple? Yes, of course.

NPM Plugins

Node Package Manager (npm) is used to install the third party modules/plugins. You can search for NPM packages from npmjs.

Installing/Creating the NPM packages requires the package.json file at the root directory of “sample.js” file to maintain the dependent files. User the npm init command to create the package.json file as shown below.

nodejs

Once the package.json file is created, we can install the package using npm install command. Here I’m installing the datetime package and using the same in “sample.js” file.

nodejs

Now let’s go and modify our “sample.js” file to make use of datetime module as shown below.

nodejs

In the above picture you can see we use the datetime module in “sample.js” and called the formatAgo() method of datetime. Here is the output of “sample.js” after using the npm package.

nodejs

Node.js HTTP Server

This title will confuse you. Node.js is not a webserver like any other IIS, Apache, etc.  But within Node.js you can create a HTTP Server which can listen the HTTP request on a specific port. Here I am going to create a Node.js HTTP Server which listens the requests on port number 8888.

nodejs

Node.js comes with default http module which is used to create the server and listen http requests. When you run the above “httpsample.js” in Node.js, http server will create inside Node.js runtime and start listening the requests as shown below.

nodejs

Whenever you make a request to server on port 8888, Node.js start responding and take control over the response. You can test this request by making a call to http://localhost:8888/

nodejs

When you stop the Node.js http server, your http://localhost:8888/ URL no more valid and doesn’t respond for your request.

That’s all for now. Hopefully this article gives you more idea about Node.js and how to use it for your development.

Happy Coding!!! smiley

Related

Microsoft Azure : Creating JAVA application & configure

We will create Java web app in Azure App Service by using the Azure portal. The Azure...

Read More >

Creating Apps Using Phonegap Framework in IOS

Creating apps for different operating system is not a reliability so for reducing developr work for ...

Read More >

NAV – Connecting Microsoft Dynamics CRM customized entities

This blog is intended to see how Microsoft Dynamics NAV (here onwards NAV) can be connected to exter...

Read More >

Windows 8 Shortcut Keys -Useful

HotkeysPressing the Windows key and some specific keys can make you life easier. Here are some usefu...

Read More >

Code Analysis & Code Metrics Integration in Azure Pipelines

This documentation shows the tutorial on how to integrate Visual Studio Code Analysis and Code Metri...

Read More >

End to End Testing Using Visual Studio 2010. (Power of Visual Studio-Ultimate for Testing. )

Scenario:Usage of Manual Test case(MTM) to perform the Load Test using Visual Studio 2010.We call it...

Read More >

XML with SQL Server

XML with SQL ServerXMLIt is all about describing data. Below is a XML which describes Patient data.1...

Read More >

CxO Roundtable at C-SOaP- SUMMIT 2013

   img{ display:block; } #outlook a{ padding:0; } body{ width:100% !importa...

Read More >

Dashboard Migrator Tool

This is a web service which helps you to copy the VSTS dashboards from 1 team to another team easing...

Read More >

Don’t Postpone Digital Transformation, but Firm up on the Strategy during COVID-19 pandemic

Trust you and your teams are safe!The recent outbreak of COVID-19 has affected lives around the worl...

Read More >

Share

Comments

Hi Sir,I was just going through blogs on JavaScript ,I found your article on node.js. Great reach for the beginners in javascript and node.js.Concepts were very clear and helpful.Keep posting articles which helps us alot in learning.The way you eloberated things with example was very much impressive.Thanks for the article sir.
9/23/2019 11:35:36 AM | Reply

Post a Comment

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

Text/HTML
Contact Us
  • *
  • *