Unit 4 Node Js
Unit 4 Node Js
Applications of Node.js
1) Streaming applications − Node.js can easily handle real-time data streams, where it is
required to download resources on-demand without overloading the server or the
user’s local machine. Node.js can also provide quick data synchronization between
the server and the client, which improves user experience by minimizing delays using
the Node.js event loop.
2) Single page apps − Node.js is an excellent choice for SPAs because of its capability to
efficiently handle asynchronous calls and heavy input/output(I/O) workloads. Data
driven SPAs built with Express.js are fast, efficient and robust.
3) Realtime applications − Node.js is ideal for building lightweight real-time
applications, like messaging apps interfaces, chatbots etc. Node.js has an event- based
architecture, as a result has an excellent WebSocket support. It facilitates real-time
two-way communication between the server and the client.
4) APIs − At the heart of Node.js is JavaScript. Hence, it becomes handling JSON data
is easier. You can therefore build REST based APIs with Node.js
Features of Node
1. Asynchronous and Event-Driven: The Node.js library’s APIs are all asynchronous
(non-blocking) in nature. A server built with Node.JS never waits for data from an API.
After accessing an API, the server moves on to the next one. In order to receive and
track responses of previous API requests, it uses a notification mechanism called
Events.
2. Single-Threaded: Node.js employs a single-threaded architecture with event looping,
making it very scalable. In contrast to typical servers, which create limited threads to
process requests, the event mechanism allows the node.js server to reply in a non-
blocking manner and makes it more scalable. When compared to traditional servers like
Apache HTTP Server, Node.js uses a single-threaded program that can handle a
considerably larger number of requests.
3. Scalable: NodeJs addresses one of the most pressing concerns in software
development: scalability. Nowadays, most organizations demand scalable software.
NodeJs can also handle concurrent requests efficiently. It has a cluster module that
manages load balancing for all CPU cores that are active. The capability of NodeJs to
partition applications horizontally is its most appealing feature. It achieves this through
the use of child processes. This allows the organizations to provide distinct app versions
to different target audiences, allowing them to cater to client preferences for
customization.
4. Quick execution of code: Node.js makes use of the V8 JavaScript Runtime motor,
which is also used by Google Chrome. Hub provides a wrapper for the JavaScript
motor, which makes the runtime motor faster. As a result, the preparation of requests
inside Node.js becomes faster as well.
5. Cross-platform compatibility: NodeJS may be used on a variety of systems,
including Windows, Unix, Linux, Mac OS X, and mobile devices. It can be paired with
the appropriate package to generate a self-sufficient executable.
6. Uses JavaScript: JavaScript is used by the Node.js library, which is another
important aspect of Node.js from the engineer’s perspective. Most of the engineers are
already familiar with JavaScript. As a result, a designer who is familiar with JavaScript
will find that working with Node.js is much easier.
7. Fast data streaming: When data is transmitted in multiple streams, processing them
takes a long time. Node.js processes data at a very fast rate. It processes and uploads a
file simultaneously, thereby saving a lot of time. As a result, NodeJs improves the
overall speed of data and video streaming.
8. No Buffering: In a Node.js application, data is never buffered. Node.js applications
are non-blocking, meaning they do not wait for I/O operations to complete before
moving on to the next task. This eliminates the need for buffering, as data is processed
in chunks as soon as it becomes available. As a result, Node.js applications can handle
large volumes of data efficiently and respond to requests quickly, making them ideal
for handling streaming data or real-time communication
9. Open Source
Node.js is an open-source platform, meaning its source code is freely available to the
public. This fosters collaboration, innovation, and community-driven development.
Anyone can contribute to improving Node.js, which has led to its rapid evolution and
widespread adoption in various industries.
setTimeout(function(){
console.log("This is the second statement");
}, 1000);
In the above case, if the timeout was set to 1000ms then also the statements will be displayed
in the same order. This is because although the callback will be immediately sent to the event
queue, the event loop won’t send it to the call stack unless the call stack is empty i.e. until the
provided input script comes to an end.
Before beginning the next run of the event loop, Node.js checks if it is waiting for any
asynchronous I/O or timers. If there aren’t any, the runtime shuts down cleanly.
Understanding how the event loop works is essential for building scalable Node.js
applications. The event loop is a fundamental part of Node.js that enables asynchronous
programming by ensuring the main thread is not blocked.
Event Emitter
• The Node.js API is based on an event-driven architecture.
• It includes the events module, which provides the capability to create and handle
custom events.
• The event module contains EventEmitter class.
• The EventEmitter object emits named events. Such events call the listener functions.
The Event Emitters have a very crucial role the Node.js ecosystem.
• Many objects in a Node emit events, for example, a net.Server object emits an event
each time a peer connects to it, or a connection is closed. The fs.readStream object emits
an event when the file is opened, closed, a read/write operation is performed. All objects
which emit events are the instances of events.EventEmitter.
Since the EvenEmitter class is defined in events module, we must include in the code with
require statement.
var events = require('events');
newListener(event, listener)
• event − String: the event name
1 • listener − Function: the event handler function
This event is emitted any time a listener is added. When this event is triggered, the
listener may not yet have been added to the array of listeners for the event.
removeListener(event, listener)
• event − String The event name
• listener − Function The event handler function
2
This event is emitted any time someone removes a listener. When this event is
triggered, the listener may not yet have been removed from the array of listeners for
the event.
Example
Const events = require('events');
const myEmitter = new events.EventEmitter();
// listener #1
var listner1 = function listner1() {
console.log('listner1 executed.');
}
// listener #2
var listner2 = function listner2() {
console.log('listner2 executed.');
}
// Bind the connection event with the listner1 function
myEmitter.addListener('connection', listner1);
// Bind the connection event with the listner2 function
myEmitter.on('connection', listner2);
// Fire the connection event
myEmitter.emit('connection');
console.log("Number of Listeners:" + myEmitter.listenerCount('connection'));
Output
listner1 executed.
listner2 executed.
Number of Listeners:2
Asynchronous programming:
• Asynchronous programming, on the other hand, refers code that doesn't execute in
sequence. These functions are performed not according to the sequence they are defined
inside a program but only when certain conditions are met.
• For example, setTimeOut() performs a task after a delay of a certain predefined number
of miliseconds.
setTimeOut(function(){
return( console.log("Hello World!") )
}, 3000)
• These functions do not run line by line but only when they are required to run,
irrespective of the function's declaration. In this case, the function runs automatically
after 3 seconds when all synchronous functions have been executed.
Package
• A package in Node.js contains all the files you need for a module.
• Modules are JavaScript libraries you can include in your project.
• Node Package Manager (NPM) is a command line tool that installs, updates or
uninstalls Node.js packages in your application.
• It is also an online repository for open-source Node.js packages.
Uninstall Packages
Use the following command to remove a local package from your project.
npm uninstall <package name>
Modules
✓ In Node.js, Modules are the blocks of encapsulated code that communicate with an
external application on the basis of their related functionality.
✓ modules can be a single file or a collection of multiple files/folders.
Core Modules: Node.js has many built-in modules that are part of the platform and come
with Node.js installation. These modules can be loaded into the program by using
the required function.
Core Modules Description
http creates an HTTP server in Node.js.
assert set of assertion functions useful for testing.
fs used to handle file system.
path includes methods to deal with file paths.
process provides information and control about the current Node.js process.
os provides information about the operating system.
querystring utility used for parsing and formatting URL query strings.
url module provides utilities for URL resolution and parsing.
http (HTTP)
This module enables you to create servers and clients that communicate using the HTTP
protocol.
The http.createServer() method allows you to create a new HTTP server instance that listens
for incoming client requests and sends appropriate responses.
const http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('Welcome to this page!');
res.end();
}).listen(3000);
url (URL)
It allows you to effortlessly parse, construct, and manipulate URLs, making it a breeze to
work with web addresses.
It offers a simple and convenient way to work with web addresses, making it easier to extract
or modify various components of a URL, such as the protocol, hostname, query parameters,
and more.
Parsing URLs:
The url.parse() method can be used to parse a URL string into a URL object.
This object contains properties representing various components of the URL, such as
protocol, hostname, path, search, and hash.
const url = require('url');
const urlString =
'<https://example.com:8080/sample?param1=value1¶m2=value2#section1>';
const parsedUrl = url.parse(urlString)
console.log(parsedUrl);
Constructing URLs:
The url.format() method used to construct a URL string from a URL object or various
components.
const url = require('url');
const urlObject = {
protocol: 'https',
hostname: 'example.com',
port: 8080,
pathname: '/sample',
query: { param1: 'value1', param2: 'value2' },
hash: '#section1'
};
const urlString = url.format(urlObject);
console.log(urlString);
Read Files
The fs.readFile() method is used to read files on your computer.
Create a Node.js file that reads the HTML file, and return the content:
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('demofile1.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
Create files
The fs.appendFile() method appends specified content to a file. If the file does not exist, the
file will be created:
var fs = require('fs');
fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) {
if (err) throw err;
console.log('Saved!');
});
The fs.open() method takes a "flag" as the second argument, if the flag is "w" for
"writing", the specified file is opened for writing. If the file does not exist, an empty
file is created:
var fs = require('fs');
fs.open('mynewfile2.txt', 'w', function (err, file) {
if (err) throw err;
console.log('Saved!');
});
The fs.writeFile() method replaces the specified file and content if it exists. If the file does
not exist, a new file, containing the specified content, will be created:
var fs = require('fs');
fs.writeFile('mynewfile3.txt', 'Hello content!', function (err) {
if (err) throw err;
console.log('Saved!');
});
Delete Files
To delete a file with the File System module, use the fs.unlink() method.
var fs = require('fs');
fs.unlink('mynewfile2.txt', function (err) {
if (err) throw err;
console.log('File deleted!');
});
Rename Files
To rename a file with the File System module, use the fs.rename() method.
var fs = require('fs');
fs.rename('mynewfile1.txt', 'myrenamedfile.txt', function (err) {
if (err) throw err;
console.log('File Renamed!');
});
Node.js OS
OS is a node module used to provide information about the computer operating
system.
It provides functions to interact with the operating system.
It provides the hostname of the operating system and returns the amount of free system
memory in bytes.
const os = require('os');
console.log("CPU architecture: " + os.arch());
console.log("Free memory: " + os.freemem());
console.log("Total memory: " + os.totalmem());
console.log('List of network Interfaces: ' + os.networkInterfaces());
console.log('OS default directory for temp files : ' + os.tmpdir());
console.log("Endianness of system: " + os.endianness());
console.log("Hostname: " + os.hostname());
console.log("Operating system name: " + os.type());
console.log('operating system platform: ' + os.platform());
console.log('OS release : ' + os.release());
Node.js Process
• The process module in Node.js allows us to get information related to the node and
control its behaviour.
• This module is globally available, and we can use it to get information such as
process ID, version, release, CPU usage, and uptime or perform actions such as
killing processes, setting uid, setting groups, unmasking, etc.
var process = require('process');
console.log('this is the working directory --> ' + process.cwd());
console.log('this is the process version --> ' + process.version);
console.log('current OS we are using --> ' + process.platform);
console.log('Feature Property: ', process.features);
Local Modules:
• Unlike built-in and external modules, local modules are created locally in your
Node.js application.
• Local module must be written in a separate JavaScript file
• first create the function then you exports using the syntax module.exports.
Hello.js
function sayhello(name)
{
console.log(`hello ${name}`)
}
module.exports=sayhello;
you can load your local module and use them in other files
test.js
const hello=require('./Hello.js')
hello('suraj')
Third-party modules:
Third-party modules are modules that are available online using the Node Package
Manager(NPM).
These modules can be installed in the project folder or globally. Some of the popular third-
party modules are Mongoose, express, angular, and React.
Example :npm install uppercase
var http=require('http')
var uc=require('uppercase')
server=http.createServer((req,res)=>
{
res.writeHead(200,{'content-type':'text/html'});
res.write(uc("test"));
res.end();
})
server.listen(8080);
Node.js Streams
• A stream is a collection of data.
• the entire data in a stream object is not stored at once in the memory. Instead, a single
chunk of data from the stream is brought into the memory, at a time.
• Node.js streams are used to read and continuously write data.
• Node.js applications are best suited for developing data streaming applications.
• Streams are the objects that facilitate you to read data from a source and write data to a
destination.
Each type of stream is an Event emitter instance and throws several events at different times.
Following are some commonly used events:
✓ Data:This event is fired when there is data available to read.
✓ End:This event is fired when there is no more data available to read.
✓ Error: This event is fired when there is any error receiving or writing data.
✓ Finish:This event is fired when all data has been flushed to underlying system.
Readable Stream
• The file object acts as a stream from which the data can be read in a chunk of specific
size.
• In the following example, we call createReadStream() function from fs module to
read data from a given file.
• The on event of the readable stream collects the file contents till the end event is fired.
var fs=require('fs')
var data="";
var readstream=fs.createReadStream('sample.txt');
readstream.setEncoding('utf-8');
readstream.on('data',function(chunk)
{
data=data+chunk;
});
readstream.on('end',function(){
console.log(data)
})
readstream.on('error',function(err)
{
console.log(err.stack);
})
Writable Stream
The createWriteStream() function from fs module creates an object of writable stream.
By using write() method, the 'fs' module can also write data to a file.
Ex:1
var fs=require('fs')
var data='this is my data ';
var writedata=fs.createWriteStream('sample.txt');
writedata.write(data,'utf8')
writedata.end();
writedata.on('finish',function()
{
console.log('data is written')
})
writedata.on('error',function(err)
{
console.log(err.stack)
})
Ex:2
After you run the file, the write.txt file will contain text from the data file.
const fs = require('fs');
const writeStream = fs.createWriteStream('write.txt', {flags: 'a'});
const data = "Using streams to write data.";
writeStream.write(data);
Ex:2
var fs = require('fs');
var readableStream = fs.createReadStream('read.txt');
var writableStream = fs.createWriteStream('write.txt');
readableStream.on('data', function(chunk) {
writableStream.write(chunk);
});
Chaining the Streams
• Chaining is a mechanism to connect the output of one stream to another stream and
create a chain of multiple stream operations.
• It is normally used with piping operations. Now we'll use piping and chaining to first
compress a file and then decompress the same.
var fs = require("fs");
var zlib = require('zlib');
// Compress the file input.txt to input.txt.gz
fs.createReadStream('input.txt')
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream('input.txt.gz'));
console.log("File Compressed.");