Unit - 2 Notes FSW
Unit - 2 Notes FSW
Unit - 2 Notes FSW
UNIT II NODE JS
Basics of Node JS – Installation – Working with Node packages –
Using Node package manager – Creating a simple Node.js application
– Using Events – Listeners –Timers - Callbacks – Handling Data I/O –
Implementing HTTP services in Node.js
Basics of Node JS
Node.js eliminates the waiting, and simply continues with the next request.
Node.js accepts the request from the clients and sends the response, while working
with the request node.js handles them with a single thread. To operate I/O operations
or requests node.js use the concept of threads. Thread is a sequence of instructions
that the server needs to perform. It runs parallel on the server to provide the
information to multiple clients. Node.js is an event loop single-threaded language. It
can handle concurrent requests with a single thread without blocking it for one
request.
Concepts of Node.js
The following diagram depicts some important parts of Node.js that are useful and
help us understand it better.
Features of Node JS
Following is a list of some important features of Node.js that makes it the first
choice of software architects.
Installation of Node.JS
Prerequisites
Hardware requirements
RAM 4GB
CPU Intel Core i3TM i3 HQ CPU @2.50 GHz
ROM 256 GB
Software requirement
Chocolatey
Installation procedure
Note: When you click on download, you will be asked to choose the file
location you want to store the instal.msi binary files. Choose a secure file
location of your choice.
Step-2: Run the installation
Once you choose the path, the next step is to double-click the
instal.msi binary files to initiate the installation process. Furthermore, you
will be asked for permission to run the application.
Warning: On the open file security warning, there is a cancel option as well.
Don't click on the cancel option, or else you may perhaps need to restart the
installation. You can use the back button to check your previous installation
step.
After clicking the next option, you will get an end-user license agreement.
Read the terms of using the software, and then click on next.
N.B. You will find a square box asking you to agree with the terms, and only
when you have read the terms, you will be allowed to proceed further.
Once you have accepted the terms and conditions, the next step is to specify
the path where you want to install node.js. Your file location is the path where
you must install the node.js in your system. Before we proceed further, you
can enroll in Web Design Training to accelerate your career.
Once you have specified the path, click on the Next button to proceed with
the installation.
Step-5: Select the default options
On proceeding with the Next option, the custom page setup will open up on
the screen. Here you will get four icons as stated below:
node.js runtime
NPM package manager
Online documentation shortcuts
Add to PATH
Select all the options as default and then proceed with the Next option.
After all these steps, you will see an install button to process the installation
of node.js on Windows. Click on the install button to initiate the installation
of node.js on your Windows. Based on your system performance it may take a
couple of minutes to install node.js on your system. Once the installation is
complete, you will get a message on your screen as — Node.js has been
successfully installed.
Step-7: Complete the installation
To verify that the node.js has been successfully installed in your system, go to
the command prompt and run it as administrator. Now use the following
command to check the node.js version installed in your system.
node -v
Copy Code
The message v14.16.1 verifies that node.js has been successfully installed on
your windows, and you are ready to use it.
Alternative Method
Do you know there is also an alternative way to download & install Node.js
and NPM on Windows 10? Using a software package manager called
Chocolatey, you can install node.js and NPM on your machine. Let us
understand how to install node.js and NPM on windows using
Chocolatey step by step. We will also look at how to uninstall the same with
Chocolatey.
Installing Chocolatey.
The first process that you need to do is to install Chocolatey in your system.
Take a note that the installation of Chocolatey requires administrator access to
the computer’s command prompt to run.
You will get a screen with execution policy changes. Select the A (Yes to
all) option and then press Enter.
To check whether you have installed it well, open the command prompt below:
choco -version
Copy Code
If you have installed it well, you will see the display of the Chocolatey
version you have installed on the screen as below:
Boom! Chocolatey is now successfully installed in your system.
Install Node.js
Using Chocolatey that you have just installed, now we can install Node.js in
the system. Open the command prompt as the administrator and put in the
Chocolatey command prompt as mentioned below:
node --version
Copy Code
If you have followed the steps correctly, the installation would have been
successful and the message below will display on your screen after the
installation has been completed.
After installing the Node.js, the Node Package Manager NPM automatically
gets installed in the system. Run the command below to check the installation
of NPM. The displayed message below verifies that the installation of NPM is
successful.
Writing your first program on node.js
Now that you have successfully downloaded and installed node.js in your
system, let’s learn how to write a program using node.js
Step-1: Create a folder and name it. (Here we have created a folder
named Node Programs.
Step-2: Open any code editor of your choice (here we are using Visual
Studio Code).
Step-3: Click on the file menu and open the folder you just created.
Step-4: Once you open the folder, create a file and name it. (Here we
have created a file named as welcome.js)
Step-5: Write your program and save it using (Ctrl + S).
Step-6: Start debugging using the shortcut f5.
});
As you can see we’re heading over a function with name requestListener. This
function will be implemented in the next step and is containing the logic to handle
incoming HTTP requests.
The created server instance is stored in server. To start the server process we need to
call server.listen. This method is receiving three arguments: port, host and a callback
function which is executed once the server process is up and running. In the example
we’re using this function to output information to the user.
res.writeHead(200);
The requestListener function is expecting to get two arguments: req and res. With
req we have access to the request object and with res we have access to the
corresponding response object.
Inside the method we’re using the res argument to first set the HTTP header status of
the response to value 200 (by using method writeHead. Second we’re using the
method end to send back a text message to the client.
res.writeHead(200);
res.end("Hello World from Node.js HTTP Server");
});
$ node server.js
To test if the server process is responding and sending back the text message let’s
use the curl command in the following way:
Handling Data I/O
Most active web applications and services have a lot of data flowing through
them. These data include text, JSON strings, binary buffers, and data streams.
Therefore, Node.js has many built-in mechanisms to handle data I/O from one
system to another.
You will almost always need to serialize JSON or JavaScript object to a JSON
string in Node. You can do so with the JSON.stringify method before writing it
to a storage device or transmitting it over the internet:
On the other hand, after reading the JSON file, you will need to deserialize the
JSON string to a plain JavaScript object using the JSON.parse method before
accessing or manipulating the data:
Create a buffer
The Buffer object is actually the most primitive memory allocation area.
Therefore, you must determine its size at the time of creation. There are 3 ways
to create a Buffer object using the new keyword:
1. new Buffer(sizeInBytes)
2. new Buffer(octetArray)
3. new Buffer(string,[encoding])
After the Buffer object has been created, you cannot expand its size, but you can
write data to any location in the buffer.
Returns a string
that contains the
characters from
the start index to
the end index to
buffer.toString([encoding],[start],
the end index of
[end])
the buffer,
decoded by the
encoding
specified by
encoding
Returns the
decoded string
stringDecoder.write(buffer)
version of the
buffer
readInt8(offset,[noAssert])
sourceBuffer[index] = destinationBuffer[index]
zlib stream
crypto stream