Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Chandigarh University, Mohali Punjab, India

Node.Js for Beginners


AMAN DEEP TIWARI : 21MCA2170
E-mail : amandeeptiawri@gmail.com

Abstract-Node.js is an open-source, cross-platform, back-end JavaScript


runtime environment that runs on the V8 engine and executes JavaScript
code outside a web browser. Node.js lets developers use JavaScript to
write command line tools and for server-side scripting—running scripts
server-side to produce dynamic web page content before the page is sent
to the user's web browser. Node.js gave rise to the Full Stack
Developers who are now able to manage server and client side by their
own. Node.js is fast and reliable for heavy files and heavy network load
applications due to its event driven, non-blocking, and asynchronous
approaches, where developers can also maintain a complete projects in
single pages (SPA) and can use for IOT
.

Introduction
Node.js is a cross platform runtime environment originally developed in
2009 by Ryan Dahl for developing server-side applications. It can be
regarded as server-side JavaScript. It was created to address the issues
platforms can have with the performance in network communication
time dedicating excessive time processing web requests and responses.
―Node.js is a platform built on Chrome’s JavaScript runtime for easily
building fast, scalable network applications. Node.js uses an event-
driven, non-blocking I/O model that makes it lightweight and efficient,
perfect for data-intensive real-time applications that run across
distributed devices.
Undoubtedly, personal computing has revolutionized the way we live
and work today. The Web has further revolutionized the way we use
applications. When it was first introduced, the Internet was designed to
present information in the form of documents. Later, JavaScript was
added, which has been the key ingredient for the innovation we see on
the Web today. Web applications are platform-independent, seamlessly
updating, safe by default, and available anytime and everywhere.
No wonder it is difficult to get started in a developer role today without
some knowledge of how the Web works.
Because of the importance of the Web and the pivotal role that
JavaScript plays in web development, you can find a solution for most
technical problems in some open source JavaScript project. Node.js
allows you to use all these innovative JavaScript projects on the server
the same as on the client browser. Using JavaScript on the server also
reduces the context switching that needs to happen in your brain as you

1|Page
change programming language and associated code conventions. This is
the emotional side of why you should use Node.js.
Node has become popular as it makes creating high performance, real-
time web applications easy. Node allows JavaScript to be used end to
end, both on the server and on the client. JavaScript has originally run
only in the web browser, but the considerable demand has brought it to
the server-side. JavaScript has developed very much and has exceled to
dominate server-side scripting. We need to analyze the security issues in
Node applications because of its use with JavaScript, which has security
liabilities. Node is event-based rather than thread-based. Node uses an
event loop within a single thread instead of multiple threads, and is able
to scale to millions of concurrent connections. In Node, a single thread
can accomplish a high concurrency. Every I/O operation in Node is
carried out in an asynchronous fashion, meaning that the server can
continue to process incoming requests while the I/O operation is taking
place Because Node is also asynchronous, AJAX could be mistakenly
considered equivalent to Node, though they are significantly different.
Node has become popular as it makes creating high performance, real-
time web applications easy. Node allows JavaScript to be used end to
end, both on the server and on the client. JavaScript has originally run
only in the web browser, but the considerable demand has brought it to
the server-side. JavaScript has developed very much and has exceled to
dominate server-side scripting. We need to analyze the security issues in
Node applications because of its use with JavaScript, which has security
liabilities. Node is event-based rather than thread-based. Node uses an
event loop within a single thread instead of multiple threads, and is able
to scale to millions of concurrent connections. In Node, a single thread
can accomplish a high concurrency. Every I/O operation in Node is
carried out in an asynchronous fashion, meaning that the server can
continue to process incoming requests while the I/O operation is taking
place. Because Node is also asynchronous, AJAX could be mistakenly

2|Page
considered equivalent to Node, though they are significantly different.
Node.js is an open-source and cross-platform JavaScript runtime
environment. It is a popular tool for almost any kind of project!
To understand how Node.js works, first you need to understand a few
key features of JavaScript that make it well suited for server-side
development.
JavaScript is a simple language, but it is also extremely flexible.

Why use Node.js


We will have a look into the real worth of Node.js in the coming
chapters, but what is it that makes this framework so famous.
Over the years, most of the applications were based on a stateless
request-response framework. In these sort of applications, it is up to the
developer to ensure the right code was put in place to ensure the state of
web session was maintained while the user was working with the
system.
But with Node.js web applications, you can now work in real-time and
have a 2-way communication. The state is maintained, and the either the
client or server can start the communication.

When to use and not use Node.js


Node.js is best for usage in streaming or event-based real-time
applications like
1. Chat applications
2. Game servers – Fast and high-performance servers that need to
processes thousands of requests at a time, then this is an ideal
framework.

3|Page
3. Good for collaborative environment – This is good for environments
which manage document. In document management environment you
will have multiple people who post their documents and do constant
changes by checking out and checking in documents. So Node.js is good
for these environments because the event loop in Node.js can be
triggered whenever documents are changed in a document managed
environment.
4. Advertisement servers – Again here you could have thousands of
request to pull advertisements from the central server and Node.js can be
an ideal framework to handle this.
5. Streaming servers – Another ideal scenario to use Node is for
multimedia streaming servers wherein clients have request's to pull
different multimedia contents from this server.
Node.js is good when you need high levels of concurrency but less
amount of dedicated CPU time.
Best of all, since Node.js is built on javascript, it's best suited when you
build client-side applications which are based on the same javascript
framework.
When to not use Node.js
Node.js can be used for a lot of applications with various purpose, the
only scenario where it should not be used is if there are long processing
times which is required by the application.
Node is structured to be single threaded. If any application is required to
carry out some long running calculations in the background. So if the
server is doing some calculation, it won't be able to process any other
requests. As discussed above, Node.js is best when processing needs less
dedicated CPU time.

4|Page
JavaScript and Node.js
JavaScript and You
Before we talk about all the technical stuff, let’s take a moment and talk
about you and your relationship with JavaScript. This chapter is here to
allow you to estimate if reading this document any further makes sense
for you.
If you are like me, you started with HTML ―development‖ long ago, by
writing HTML documents. You came along this funny thing called
JavaScript, but you only used it in a very basic way, adding interactivity
to your web pages every now and then.
What you really wanted was ―the real thing‖, you wanted to know how
to build complex web sites you learned a programming language like
PHP, Ruby, Java, and started writing ―backend‖ code.
Nevertheless, you kept an eye on JavaScript, you saw that with the
introduction of jQuery, Prototype and the likes, things got more
advanced in JavaScript land, and that this language really was about
more than window.open().
However, this was all still frontend stuff, and although it was nice to
have jQuery at your disposal whenever you felt like spicing up a web
page, at the end of the day you were, at best, a JavaScript user, but not a
JavaScript developer.
And then came Node.js. JavaScript on the server, how cool is that?
You decided that it’s about time to check out the old, new JavaScript.
But wait, writing Node.js applications is the one thing; understanding
why they need to be written the way they are written means -
understanding JavaScript. And this time for real.
Here is the problem: Because JavaScript really lives two, maybe even
three lives (the funny little DHMTL helper from the mid-90’s, the more

5|Page
serious frontend stuff like jQuery and the likes, and now server-side),
it’s not that easy to find information that helps you to learn JavaScript
the ―right‖ way, in order to write Node.js applications in a fashion that
makes you feel you are not just using JavaScript, you are actually
developing it.
Because that’s the catch: you already are an experienced developer, you
don’t want to learn a newtechnique by just hacking around and mis-
using it; you want to be sure that you are approaching it from the right
angle.

Server-side JavaScript
JavaScript defines what you can do with the language, but it doesn’t say
much about what the language itself can do. JavaScript is a ―complete‖
language: you can use it in many contexts and achieve everything with it
you can achieve with any other ―complete‖ language.
Node.js really is just another context: it allows you to run JavaScript
code in the backend, outside a browser.In order to execute the JavaScript
you intend to run in the backend, it needs to be interpreted and,well,
executed. This is what Node.js does, by making use of Google’s V8
VM, the same runtime environment for JavaScript that Google Chrome
uses.
Plus, Node.js ships with a lot of useful modules, so you don’t have to
write everything from scratch,
like for example something that outputs a string on the console.
Thus, Node.js is really two things: a runtime environment and a library.
Download & Install Node.js
To start building your Node.js applications, the first step is the
installation of the node.js framework. The Node.js framework is

6|Page
available for a variety of operating systems right from Windows to
Ubuntu and OS X. Once the Node.js framework is installed you can start
building your first Node.js applications. Node.js also has the ability to
embedded external functionality or extended functionality by making
use of custom modules. These modules have to be installed separately.
An example of a module is the MongoDB module which allows you to
work with MongoDB databases from your Node.js application
How to install node.js
To start building your Node.js applications, the first step is the
installation of the node.js framework. The Node.js framework is
available for a variety of operating systems right from Windows to
Ubuntu and OS X. Once the Node.js framework is installed you can start
building your first Node.js applications.
Node.js also has the ability to embedded external functionality or
extended functionality by making use of custom modules. These
modules have to be installed separately. An example of a module is the
MongoDB module which allows you to work with MongoDB databases
from your Node.js application.
The first steps in using Node.js is the installation of the Node.js
libraries on the client system. To perform the installation of Node.js,
perform the below steps;

Install Node.js on Windows

Visit Node.js official web site https://nodejs.org. It will automatically


detect OS and display download link as per your Operating System.
For example, it will display following download link for 64 bit
Windows OS.

7|Page
Download Node.JS Installer for Windows

Download the installer for windows by clicking on LTS or Current


version button. Here, we will install the latest version LTS for
windows that has long time support. However, you can also install the
Current version which will have the latest features.

After you download the MSI, double-click on it to start the installation


as shown below.

8|Page
Node.js Installation

Click Next to read and accept the License Agreement and then click
Install. It will install Node.js quickly on your computer. Finally, click
finish to complete the installation.
Verify Installation

Once you install Node.js on your computer, you can verify it by


opening the command prompt and typing node -v. If Node.js is
installed successfully then it will display the version of the Node.js
installed on your machine, as shown below.

9|Page
Verify Node.js Installation

Install Node.js on Mac/Linux

Visit Node.js official web site https://nodejs.org/en/download page.


Click on the appropriate installer for Mac (.pkg or .tar.gz) or Linux to
download the Node.js installer.

Node Environment Setup

10 | P a g e
Once downloaded, click on the installer to start the Node.js installation
wizard. Click on Continue and follow the steps. After successful
installation, it will display summary of installation about the location
where it installed Node.js and NPM.

Node.js Installation on OS X

After installation, verify the Node.js installation using terminal


window and enter the following command. It will display the version
number of Node.js installed on your Mac.

Optionally, for Mac or Linux users, you can directly install Node.js
from the command line using Homebrew package manager for Mac OS
or Linuxbrew package manager for Linux Operating System. For
Linux, you will need to install additional dependencies, viz. Ruby
11 | P a g e
version 1.8.6 or higher and GCC version 4.2 or higher before installing
node.

Running your first Hello world application in Node.js

Once you have downloaded and installed Node.js on your computer,


lets try to display "HelloWorld" in a web browser.

Create file Node.js with file name firstprogram.js

Code Explanation:

1. The basic functionality of the "require" function is that it reads a


JavaScript file, executes the file, and then proceeds to return an object.
Using this object, one can then use the various functionalities available
in the module called by the require function. So in our case, since we
want to use the functionality of http and we are using the require(http)
command.

2. In this 2nd line of code, we are creating a server application which is


based on a simple function. This function is called, whenever a request
is made to our server application.

3. When a request is received, we are asking our function to return a


"Hello World" response to the client. The writeHead function is used
to send header data to the client and while the end function will close
the connection to the client.

12 | P a g e
4. We are then using the .listen function to make our server application
listen to client requests on port no 8080. You can specify any available
port over here.

Executing the code

Step 1) Save the file on your computer: C:\Users\Your Name\


firstprogram.js

Step 2) In the command prompt, navigate to the folder where the fike
is stored. Enter the command

Node firstprogram.js

Step 3) Now, your computer works as a server! If anyone tries to


access your computer on port 8080, they will get a "Hello World!"
message in return!

Step 4) Start your internet browser, and type in the address:


http://localhost:8080

Summary
 This paper presents the framework using node.js to build highly scalable
and high-speed backend database server for web developers as well as
application developers. It also demonstrates why we use Node.js and
When to use and not use Node.js.

13 | P a g e
 We have seen the installation of Node.js via the msi installation
module which is available on the Node.js website. This installation
installs the necessary modules which are required to run a Node.js
application on the client.

 A simple Node.js application consists of creating a server which listens


on a particular port. When a request comes to the server, the client
automatically sends a 'Hello World' response to the client.

ACKNOWLEDGEMENT
I would like to thank Prof. Dr Jarnail Singh
Department: University Institute of Computing,Chandigarh
University, Mohali Punjab, India for providing the support for this
research work and their valuable guidance.

REFERENCES
 https://nodejs.dev/learn
 https://www.tutorialsteacher.com/nodejs/nodejs-resources
 https://www.javatpoint.com/nodejs-tutorial
 https://nodejs.org/en/
 https://en.wikipedia.org/wiki/Node.js
 Sunil L. Bangare Associate Professor - I. T. Dept. & Research
consultant (Open for collaboration-Using Node.Js to Build High
Speed and Scalable Backend Database Server

 Manuel Kiessling-The Node Beginner Book A comprehensive Node.js


tutorial

14 | P a g e
 Basarat Ali Syed-Beginning Node.js

 Krishna Rungta -Learn NodeJS in 1 Day


 Nimesh Chhetri -A Comparative Analysis of Node.js (Server-Side
JavaScript)

15 | P a g e

You might also like