WDF Unit Ii Notes
WDF Unit Ii Notes
UNIT II - NODE JS
Understanding Node JS - Installing Node.js - Working with Node packages - Creating a simple Node.js
application - Handling Data I/O - Using the Buffer Module to Buffer Data - Implementing HTTP services in
Node.js - Processing URLs - Processing Query Strings and Form Parameters - Understanding Request,
Response, and Server Objects.
2.1. Understanding the Node.js:
Node.js was developed in 2009 by Ryan Dahl as an answer to the frustration caused by
concurrency issues, especially when dealing with web services. Google had just come out with
the V8 JavaScript engine for the Chrome web browser, which was highly optimized for web
traffic. Dahl created Node.js on top of V8 as a server-side environment that matched the client-
side environment in the browser.
The result is an extremely scalable server-side environment that allows developers to more
easily bridge the gap between client and server. The fact that Node.js is written in JavaScript
allows developers to easily navigate back and forth between client and server code and even
reuse code between the two environments.
Node.js has a great ecosystem with new extensions being written all the time. The Node.js
environment is clean and easy to install, configure, and deploy. Literally in only an hour or two
you can have a Node.js webserver up and running.
Node.js is an open-source server-side runtime environment built on Chrome’s V8 JavaScript
engine.
It provides an event-driven, non-blocking (asynchronous) I/O and cross-platform runtime
environment for building highly scalable server-side applications using JavaScript.
Who Uses Node.js?
Node.js quickly gained popularity among a wide variety of companies. These companies
use Node.js first and foremost for scalability but also for ease of maintenance and faster
development. The following are just a few of the companies using the Node.js technology:
■ Yahoo!
■ LinkedIn
■ eBay
■ New York Times
■ Dow Jones
■ Microsoft
What Is Node.js Used For?
Node.js can be used for a wide variety of purposes. Because it is based on V8 and has highly
optimized code to handle HTTP traffic, the most common use is as a webserver. However,
Node.js can also be used for a variety of other web services such as:
■ Web services APIs such as REST
■ Real-time multiplayer games
■ Backend web services such as cross-domain, server-side requests
■ Web-based applications
■ Multiclient communication such as IM
What Does Node.js Come With?
Node.js comes with many built-in modules available right out of the box.
■ Assertion testing: Allows you to test functionality within your code.
■ Buffer: Enables interaction with TCP streams and file system operations.
■ C/C++ add-ons: Allows for C or C++ code to be used just like any other Node.js module.
■ Child processes: Allows you to create child processes.
■ Cluster: Enables the use of multicore systems.
■ Command line options: Gives you Node.js commands to use from a terminal.
■ Console: Gives the user a debugging console.
■ Crypto: Allows for the creation of custom encryption.
■ Debugger: Allows debugging of a Node.js file.
■ DNS: Allows connections to DNS servers.
■ Errors: Allows for the handling of errors.
■ Events: Enables the handling of asynchronous events.
■ File system: Allows for file I/O with both synchronous and asynchronous methods.
■ Globals: Makes frequently used modules available without having to include them first.
■ HTTP: Enables support for many HTTP features.
■ HTTPS: Enables HTTP over the TLS/SSL.
■ Modules: Provides the module loading system for Node.js.
■ Net: Allows the creation of servers and clients.
■ OS: Allows access to the operating system that Node.js is running on.
■ Path: Enables access to file and directory paths.
■ Process: Provides information and allows control over the current Node.js process.
■ Query strings: Allows for parsing and formatting URL queries.
■ Readline: Enables an interface to read from a data stream.
■ REPL: Allows developers to create a command shell.
■ Stream: Provides an API to build objects with the stream interface.
■ String decoder: Provides an API to decode buffer objects into strings.
■ Timers: Allows for scheduling functions to be called in the future.
■ TLS/SSL: Implements TLS and SSL protocols.
■ URL: Enables URL resolution and parsing.
■ Utilities: Provides support for various apps and modules.
■ V8: Exposes APIs for the Node.js version of V8.
■ VM: Allows for a V8 virtual machine to run and compile code.
■ ZLIB: Enables compression using Gzip and Deflate/Inflate.
Node.js can be used to build different types of applications such as command line application,
web application, real-time chat application, REST API server, chat rooms, web streaming
options, for collection of data, data processing, browser games, high-speed applications, the
databases that are expected to be scalable/expandable eventually. Application program
interfaces applied on the databases, queueing applications like message queues, etc.
Requests – Based on the specific tasks users need to perform in a web application, the requests
can either be blocking (complex) or non-blocking (simple).
Node.js server – It accepts user requests, processes them, and returns the results to the
corresponding users.
Event queue – It stores the incoming requests and passes them sequentially to the Event Pool.
Event loop – After receiving the client requests from the event queue, it sends responses to
corresponding clients.
Thread pool – It contains the threads available for performing those operations necessary to
process requests.
External resources – The external resources are used for blocking client requests and can be
used for computation, data storage, and more.
After choosing the path, double click to install .msi binary files to initiate the installation
process. Then give access to run the application.
Clicking on the Next button, a custom page setup page will be opened. Choose npm package
manager, not the default of Node.js runtime. This way, Node and NPM can be installed
simultaneously.
143MB of space is required to install Node.js and npm features.
The following features will be installed by default:
Node.js runtime
Npm package manager
Online documentation shortcuts
Add to Path
Click the Install button to start Installing node.js on Windows
Step 3 : Check Nodejs and NPM Version
Click on the “ macOS Installer ” option to download the .pkg installer. Make sure you
download it to your desired location.
Go to Node.js official website. -> https://nodejs.org/en
Click the “Download” button on the Homepage.
Download the recommended LTS(Long-Term Support) version for MacOS on the download
page.
After the installation has completed, finalize it by clicking on a button such as “Finish”,
“Close” or “Complete Installation”.
Step 3: Verify Node.js installation
To validate that Node.js installation is successful, open a terminal and run the command.
Check the installed Node.js version.
Run the "node -v" command in your terminal to confirm node installation.
Step 4: Update your NPM Version
$ sudo npm install npm -g // global
2.3. Working with Node packages
1. NODE PACKAGE MANAGER:
The NPM -Node Package Manager is the default package manager for Node.js
Basically, NodeJs is a JavaScript runtime environment, allowing developers to develop the
scalable application in a given time.
NPM allows developers to easily install, share, and manage libraries and tools needed for their
JavaScript applications.
NPM allows open-source web developers to share and borrow packages for app development.
It works as a command-line utility for the application for installing packages in the project,
dependency management, and even version management.
It is a dependency and module manager for the ecosystem, similar to other managers such as
composer in PHP or pip in Python.
Node Package Manager provides two main functionalities:
It provides online repositories for node.js packages/modules which are searchable on
search.nodejs.org
It also provides command line utility to install Node.js packages, do version management
and dependency management of Node.js packages.
//Components of NPM:
Website: Packages can be found from the official NPM website for the project. Also, create and
set up profiles to manage and access all types of packages.
Command Line Interface (CLI): To Interface with NPM packages and repositories, the CLI
runs from your computer's terminal.
Registry: It has a huge database of JavaScript projects and meta-data. Thus it allows to use
any supported NPM registry. Also, it is possible to utilise someone else's registry as per their
terms of use.
It gets installed into the system with installation of Node.js. The required packages and
modules in Node project are installed using NPM.
A package contains all the files needed for a module and modules are the JavaScript libraries
that can be included in Node project according to the requirement of the project.
NPM can install all the dependencies of a project through the package.json file. It can also
update and uninstall packages.
To check NPM Version
npm -v
To update the NPM Version
npm update npm@latest-g
As npm is a global package, -g flag is used to update it globally.
To access NPM help, write npm help in the command prompt or terminal window
npm help
2.WORKING WITH NODE PACKAGES:
A Node Packaged Module is a packaged library that can easily be shared, reused, and installed
in different projects. Many different modules are available for a variety of purposes.
For example, the Mongoose module provides an ODM (Operational Data Model) for
MongoDB, Express extends Node’s HTTP capabilities, and so on.
Node.js modules are created by various third-party organizations to provide the needed features
that Node.js lacks out of the box. This community of contributors is active in adding and
updating modules.
Node Packaged Modules include a package.json file that defines the packages. The package.json
file includes informational metadata, such as the name, version author, and contributors, as well
as control metadata, such as dependencies and other requirements that the Node Package
Manager uses when performing actions such as installation and publishing.
Understanding the Node Package Registry
The Node modules have a managed location called the Node Package Registry where packages
are registered. This allows to publish own packages in a location where others can use them as
well as download packages that others have created.
The Node Package Registry is located at https://npmjs.com. From this location ,can view
the newest and most popular modules as well as search for specific packages, as shown in below
👇
The dependency hierarchy is listed; some of the methods Express requires are cookie-
signature, range-parser, debug, fresh, cookie, and send modules. Each of these was
downloaded during the install. Notice that the version of each dependency module is listed.
Node.js has to be able to handle dependency conflicts. For example, the express module
requires cookie 0.3.1, but another module may require cookie 0.3.0. To handle this
situation, a separate copy for the cookie module is placed in each module’s folder under
another node_modules folder.
To illustrate how modules are stored in a hierarchy, consider the following example of how
express looks on disk. Notice that the cookie and send modules are located under the
express module hierarchy, and that since the send module requires mime it is located under
the send hierarchy:
./
./node_modules
./node_modules/express
./node_modules/express/node_modules/cookie
./node_modules/express/node_modules/send
./node_modules/express/node_modules/send/node_modules/mime
A great way to use package.json files is to automatically download and install the dependencies
for your Node.js app. Create a package.json file in the root of your project code and add the
necessary dependencies to it. For example, the following package.json requires the express
module as a dependency.
{ "name":
"my_module",
"version": "0.1.0",
"dependencies" : {
"express" : "latest"
}
}
Then you run the following command from root of your package, and the express module is
automatically installed.
npm install
No module is specified in the npm install because npm looks for a package.json file by
default.To add the additional modules, add those to the dependencies directive and then run
npm install again.
//CREATE A PACKAGE.JSON FILE
Create a package.json file to store useful metadata about the project and help to manage the
project's dependent Node.js modules.
This is a JSON (JavaScript Object Notation) file which is a standard format used for sharing,
based on JavaScript objects and consisting of data stored as key.value pairs.
A package.json file can be created in two ways:
1. Using npm init: Running this command, system expects user to fill the vital information
required. It provides users with default values which are editable by the user
npm init
This will open a CLI prompt that will ask you a few questions to help fill in your new
package.json file.
This utility will walk you through creating a package.json file.
It only covers the most common items and tries to guess sensible defaults.
See 'npm help init' for definitive documentation on these fields and exactly what they do.
Use 'npm install <pkg> 'afterwards to install a package and save it as a dependency in the
package.json file.
Press ^C at any time to quit.
package name:
It will ask information like the name of your project, version, description, entry point, test
command, git repository, keywords, author, and license.
Hit center to accept the default value or you can type in your own value if you want to
change it.
Once you finish with the questions, you'll be prompted to save the file.
package.json will be generated when npm init is run to initialise a JavaScript/ Node.js
project, with these basic metadata provided by developers:
name: Name represents the names of the project. The name must be:
one word
lower case
not start with an ''_" or '.'
version: The version denotes the current version of the modules. It must follow the
semantic versioning guidelines, e.g., 1.0.0
description: The description provides more information about the project.
• Engines: The engine property i is an object used to specify the library versions
required for the application to run.
license: the project's license
Scripts: Scripts are key-value pairs used to perform a series of tasks such as building,
testing, etc. %
Main: Main is the entry point of the application and should refer to main page of your
app.
Homepage: Homepage is used to specify the landing page of the app.
Example: package.json file-1
{
"name":"',
"version": "1.0.0",
"description":"",
"main": "index.js",
"scripts": {
"Test": "echo "Error: no test specified\" &&exit 1"
}
"author":"',
"license": "ISC"
Installing all dependencies
To install any dependencies type npm install in the root of the project, and it will automatically
alter the package.json file to include the dependencies, or we ca manually edit the file yourself,
then run npm install to install the dependencies.
npm install
Dependencies: It denotes the list of modules and packages required for the application to
function. It is added to the dependency list when installed.
dev Dependencies: are modules that are not required to run the application but are needed in
the development phase.
Controlling where the package gets installed:
To install a package and simultaneously save it in package.json file (in case using Node.js), add
-save flag. The-save flag is default in npm install command so it is equal so npm install
package_name command.
Examples:
npm install express_save
By-save flag one can control where the packages are to be installed.
-save-prod: Using this packages will appear in Dependencies which is also by
default.
-save-dev: Using this packages will get appear in dev Dependencies and only be
used in the development mode
Examples:
npm install node-color-save-dev
Example package.json file 2: This is an example that contains all of the properties of
json file{
"name": "packnge.json-educative",
"version": "1.0.0",
"description": "Master file of packnge.json",
"private": true,
"main": "index.js",
"scripts": {
"start": "node index",
"dev": "nodemon index"
},
"repository": {
"type": "git",
"url": "https://github.com/Educative/package.json-educative.git"
},
"keywords": [
"node",
"package.json",
"javascript",
"npm"
],
"author": "Sahara",
"license": "Standard",
"homepage": "https://github.com/Educative/packagejson-educative#readme",
"engines": {
"npm": "6.10.4",
"node": "10.14.3"
},
"dependencies": {},
"devDependencies": {}
}
2.4. Creating a simple Node.js application
To build robust and performant web applications with Node.js, developers must understand
these data-handling mechanisms. This knowledge ensures that applications can process data
efficiently, making them more responsive and scalable.
Manipulating JSON data: How to work with JSON strings, converting them to objects and vice
versa.
Managing binary data buffers: Handling raw binary data efficiently using Node.js buffers.
Implementing readable and writable streams: Creating streams for continuous data flow,
which is crucial for handling large datasets or real-time data.
Compressing and decompressing data: Techniques to reduce data size for storage or
transmission and to restore it to its original form.
1. Introduction to JSON : JSON is widely used in Node.js applications to represent and exchange
data. It’s a standard format for data interchange, particularly in web applications and APIs.
2. JSON Serialization and Deserialization : JSON allows you to convert JavaScript objects into a
string format using JSON.stringify() for transmission or storage. Similarly, you can convert
the string back into an object using JSON.parse().
Serialization is the process of converting objects into a format suitable for storage or
transmission. JSON is used for serializing and deserializing data to facilitate communication
between different parts of a system (e.g., between a web client and server or between services).
3. Advantages of JSON Over XML:
"JSON is much more efficient and takes up fewer characters." : JSON syntax is more
concise compared to XML. For example, JSON uses fewer characters for the same data
representation, reducing the amount of data transmitted.
"Serializing/deserializing JSON is faster than XML because it’s simpler syntax." : JSON’s
simpler syntax means that operations to convert data to and from JSON format are
generally faster than those for XML.
"JSON is easier to read from a developer’s perspective because it is similar to JavaScript
syntax." : JSON’s syntax closely resembles JavaScript object notation, making it easier for
developers familiar with JavaScript to read and write JSON data.
4. When to Use XML
XML may be preferred in cases where:
1. Complex Objects: XML can represent more complex structures and metadata that
JSON might not handle as well.
2. Existing Infrastructure: You might continue using XML if your system already
relies on XML-based transformations (e.g., XSLT) or has extensive XML-based
processes in place.
1. JSON to JavaScript Object Conversion:
A JSON string represents the JavaScript object in string form. The string syntax is similar
to code, making it easy to understand
the JSON.parse(string) method to convert a string that is properly formatted with JSON
into a JavaScript object.
1. Example of Converting JSON to a JavaScript Object
The example demonstrates how to define accountStr as a JSON string and convert it into a
JavaScript object using JSON.parse(). Once converted, the properties of the object can be
accessed using JavaScript's dot notation.
var accountStr = '{"name":"Jedi", "members":["Yoda","Obi Wan"], "number":34512,
"location": "A galaxy far, far away"}';
var accountObj = JSON.parse(accountStr);
console.log(accountObj.name);
console.log(accountObj.members);
Step 1: accountStr is a JSON string that represents an object with properties: name,
members, number, and location.
Step 2: accountObj = JSON.parse(accountStr); converts the JSON string accountStr into a
JavaScript object accountObj.
Step 3: console.log(accountObj.name); prints the value of the name property of
accountObj, which is "Jedi".
Step 4: console.log(accountObj.members); prints the members array, which contains
"Yoda" and "Obi Wan".
OUTPUT:
Jedi
[ 'Yoda', 'Obi Wan' ]
The first console.log(accountObj.name); outputs "Jedi", which is the value associated
with the name property in the object.
The second console.log(accountObj.members); outputs ['Yoda', 'Obi Wan'], the array
stored in the members property, listing the members as an array of strings.
2. Converting JavaScript Objects to JSON
Node allows to convert a JavaScript object into a properly formatted JSON string. Thus the
string form can be stored in a file or database, sent across an HTTP connection, or written to
a stream/buffer. Use the JSON.stringify(text) method to parse JSON text and generate a
JavaScript object:
For example, the following code defines a JavaScript object that includes string, numeric,
and array properties. Using JSON.stringify(), it is all converted to a JSON string:
var accountObj = {
name: "Baggins",
number: 10645,
members: ["Frodo, Bilbo"],
location: "Shire"
};
var accountStr = JSON.stringify(accountObj);
console.log(accountStr);
1. Defining a JavaScript Object:
var accountObj = {
name: "Baggins",
number: 10645,
members: ["Frodo, Bilbo"],
location: "Shire"
};
accountObj is a JavaScript object.
It has four properties:
name: A string value "Baggins".
number: A numeric value 10645.
members: An array containing a single string "Frodo, Bilbo".
location: A string value "Shire".
2. Converting the Object to a JSON String:
var accountStr = JSON.stringify(accountObj);
JSON.stringify(accountObj) converts the JavaScript object accountObj into a
JSON string.
The resulting JSON string, stored in accountStr, is a text-based representation of
the object.
3. Logging the JSON String to the Console:
console.log(accountStr);
The console.log() function is used to print the JSON string to the console.
The preceding code outputs the following:
{"name":"Baggins","number":10645,"members":["Frodo, Bilbo"],"location":"Shire"}
While JavaScript is Unicode friendly, it is not good at managing binary data. However, binary
data is useful when implementing some web applications and services. For example:
1. Transferring compressed files
2. Generating dynamic images
3. Sending serialized binary data
Understanding Buffered Data
Buffered data is made up of a series of octets in big endian or little endian format. That means
they take up considerably less space than textual data. Therefore, Node.js provides the Buffer
module that gives the functionality to create, read, write, and manipulate binary data in a
buffer structure. The Buffer module is global, so do not need to use the require() statement to
access it.
Buffered data is stored in a structure similar to that of an array but is stored outside the
normal V8 heap in raw memory allocations. Therefore a Buffer cannot be resized.
When converting buffers to and from strings, need to specify the explicit encoding method to
be used.
Methods of encoding between strings and binary buffers:
METHOD DESCRIPTION
utf8 Multi-byte encoded Unicode characters
used as the standard in most documents
and webpages.
utf16le Little endian encoded Unicode
characters of 2 or 4 bytes.
ucs2 Same as utf16le.
base64 Base64 string encoding.
Hex Encode each byte as two hexadecimal
characters.
QUESTIONS TO REVIEW:
1. What are module and what are some of the built-in modules provided by Node.js? Provide a brief
description of at least five built-in modules?
1. In Node.js, a module is a reusable piece of code that can be imported into other files in your
application.
2. It helps organize and structure your code into manageable, logical parts.There are two main
types of modules in Node.js:
1. Core Modules : are built into Node.js.
2. Local Modules : These are custom modules we create .
List of built in Modules:
Node.js comes with many built-in modules available right out of the box.
■ Assertion testing: Allows you to test functionality within your code.
■ Buffer: Enables interaction with TCP streams and file system operations.
■ C/C++ add-ons: Allows for C or C++ code to be used just like any other Node.js module.
■ Child processes: Allows you to create child processes.
■ Cluster: Enables the use of multicore systems.
■ Command line options: Gives you Node.js commands to use from a terminal.
■ Console: Gives the user a debugging console.
■ Crypto: Allows for the creation of custom encryption.
■ Debugger: Allows debugging of a Node.js file.
■ DNS: Allows connections to DNS servers.
■ Errors: Allows for the handling of errors.
■ Events: Enables the handling of asynchronous events.
■ File system: Allows for file I/O with both synchronous and asynchronous methods.
■ Globals: Makes frequently used modules available without having to include them first.
■ HTTP: Enables support for many HTTP features.
■ HTTPS: Enables HTTP over the TLS/SSL.
■ Modules: Provides the module loading system for Node.js.
■ Net: Allows the creation of servers and clients.
■ OS: Allows access to the operating system that Node.js is running on.
■ Path: Enables access to file and directory paths.
■ Process: Provides information and allows control over the current Node.js process.
■ Query strings: Allows for parsing and formatting URL queries.
■ Readline: Enables an interface to read from a data stream.
■ REPL: Allows developers to create a command shell.
■ Stream: Provides an API to build objects with the stream interface.
■ String decoder: Provides an API to decode buffer objects into strings.
■ Timers: Allows for scheduling functions to be called in the future.
■ TLS/SSL: Implements TLS and SSL protocols.
■ URL: Enables URL resolution and parsing.
■ Utilities: Provides support for various apps and modules.
■ V8: Exposes APIs for the Node.js version of V8.
■ VM: Allows for a V8 virtual machine to run and compile code.
■ ZLIB: Enables compression using Gzip and Deflate/Inflate.
1. fs (File System) :
Purpose: To work with the file system (e.g., reading, writing, deleting files).
Common Functions:
1. fs.readFile(): Asynchronously reads the contents of a file.
2. fs.writeFile(): Asynchronously writes data to a file.
3. fs.appendFile(): Asynchronously appends data to a file.
4. fs.unlink(): Asynchronously deletes a file.
2. http:
Purpose: To create and manage HTTP servers and handle HTTP requests and responses.
Common Functions:
http.createServer(): Creates an HTTP server.
server.listen(): Binds and listens for connections on a specified port.
3. path:
Purpose: To work with file and directory paths in a cross-platform way.
Common Functions:
path.join(): Joins multiple path segments into a single path.
path.resolve(): Resolves a sequence of paths into an absolute path.
path.basename(): Returns the last portion of a path.
path.extname(): Returns the extension of the path.
4. url:
Purpose: To parse and format URL strings.
Common Functions:
url.parse(): Parses a URL string into its components.
url.format(): Formats a URL object into a URL string.
5. stream:
Purpose: To work with streams, which are data-handling methods that allow for efficient
processing of large amounts of data.
Common Types:
Readable: Streams from which data can be read.
Writable: Streams to which data can be written.
Duplex: Streams that are both readable and writable.
Transform: Streams that can modify or transform the data as it is written and read.
2. Describe the step-by-step process of setting up Node.js on your computer and creating a simple
Node.js application. Include the necessary commands and code snippets for reference.
1. Installing node.js on your computer:
Installing Node.js on your system depends on the operating system you're using. Below are step-by-
step instructions for installing Node.js on different platforms:
1. Windows
Option 1: Using the Node.js Installer
i. Download the Installer: Go to the official Node.js website.
Download the LTS (Long Term Support) version for Windows (recommended for most users).
ii. Run the Installer:
Double-click the downloaded .msi file.
Follow the installation prompts. It's recommended to install the default settings, which include
installing npm (Node Package Manager) alongside Node.js.
iii. Verify the Installation: