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

Volume Management in Kubernetes

Since pods created in k8s are ephemeral, we are able to get the data as long as pods are alive, but ...

Read More >

How to print Header and Details (Lines) in different pages of a RDLC Report in NAV 2013 R2

We usually print header and details section of a document on same page, what if we want to print on ...

Read More >

Performing Sonar Analysis for .NET projects

 Sonar Integration with TFS build for .net projectsThis document provides details about TFS and...

Read More >

Why customer should choose Azure DevOps

Azure DevOps is rebranding of Microsoft’s Visual Studio Team Services (VSTS), which is the onl...

Read More >

Microsoft Office 2013 Features and Looks

Now Office 2013 launched recently, now its not available for users may be its available for User aft...

Read More >

A Brief History of Time (spent in the ERP marketplace)

Confession: This blog is no way connected to book titled "A Brief History of Time", author...

Read More >

To recover the space occupied by TFS 2010, after deleting the artifacts.

When you delete the team project or project artifacts from TFS 2010, the TFS collection database won...

Read More >

How to upload the files in Selenium using AutoIt

Selenium   WebDriver Test Automation framework allows us to write the automation scripts f...

Read More >

Ansible Playbooks

Playbooks are nothing but files consisting of your written code, and they are written in YAML langua...

Read More >

Namespaces in Kubernetes

A default namespace is created automatically when the cluster is being setup. To isolate or prevent ...

Read More >

Share

Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

Text/HTML
Text/HTML
Contact Us
  • *
  • *