www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Webpage
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Agenda
❖ Client Server Architecture
❖ Limitations of Multi–Threaded Model
❖ What is Node.js?
❖ Features of Node.js
❖ Node.js Installation
❖ Blocking Vs. Non – Blocking I/O
❖ Creating Node.js First Program
❖ Node.js Modules
❖ Demo – Grocery List Web Application using Node.js
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Client – Server Architecture
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Client Server Architecture
Client 1
Client 2
Client 3
Server Database
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Traditional Multi-Threaded Model
Handle Request
A
Handle Request
B
Handle Request
C
Read ResponseRead Request
Assign Thread for A
Assign Thread for C
Assign Thread for B
Thread
Thread
Thread
User HTTP Request A
User HTTP Request B
User HTTP Request C
User HTTP Request D
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Limitations
of
Multi – Threaded Approach
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Limitations of Multi-Threaded Models
Shared
Resources
Updating
Resources
Wants to
Update
Wants to
Update
Thread A Thread C
Thread B
➢ In a multi-threaded HTTP server, for each and every request that the server receives, it creates a separate thread
which handles that request
➢ If a request acquires a lock in the shared resource and it is ‘exclusive’, it will affect result of other requests
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
High chances of deadlock if application
is not designed properly
Limitations of Multi-Threaded Models
Web Servers need to handle thousands
of HTTP requests. So, many threads may
have to wait for network operations
For thousands of simultaneous requests,
spawning threads for all processes would
not achieve the desired scalability
Scalability
Complex
Deadlock
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
What is Node.js ?
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
What is Node.js?
➢Node.js is an open source runtime environment for server-side and networking applications and is single threaded.
➢Uses Google JavaScript V8 Engine to execute code.
➢It is cross platform environment and can run on OS X, Microsoft Windows, Linux, FreeBSD, NonStop and IBM.
➢Provides an event driven architecture and non blocking I/O that is optimized and scalable.
N O D E
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Features of Node.js
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Features of Node.js
1. Asynchronous
Caller Recipient
request
response
callback
➢ When request is made to server, instead of waiting for the request to complete, server continues to
process other requests
➢ When request processing completes, the response is sent to caller using callback mechanism
Asynchronous Model
Asynchronous
Event Driven
Very Fast
Scalable
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Features of Node.js
➢ When request is made to server, instead of waiting for the request to complete, server continues to
process other requests
➢ When request processing completes, the response is sent to caller using callback mechanism
Event Emitters
Event Loop
(Single - threaded)
File System
Network
Process
Other
Event Queue
Thread Pool
2. Single Threaded and Event Driven:
Asynchronous
Event Driven
Very Fast
Scalable
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Features of Node.js
3. Very Fast
4. Single Threaded but Highly Scalable
Being built on Google Chrome's V8 JavaScript Engine, Node.js library is very fast in code
execution.
Node.js is highly scalable because event mechanism helps the server to respond in a non-
blocking way.
Asynchronous
Event Driven
Very Fast
Scalable
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Node.js Installation
EDUREKA NODE JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Node.js Installation
Go to https://nodejs.org/en/1
Download and Install2
EDUREKA NODE JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Node.js Installation
Open node.js command prompt3
➢ node –version
Check the version of Node.js
installed
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Creating Your First Node.js Program
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Node.js First Program
Create a Directory: mkdir node_example1
Create a File: first_example.js2
Go to the node_example directory3
Execute the java script file: node first_example.js4
2
1
3
4
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Simple Web Application Example
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Simple Web Application Example
Example:
Importing Module:
‘require’ is used to load a Node.js modules.
1
Example:
Creating Server
➢ Used to create web server object
➢ Function (request, response) is called once for
every HTTP request to the server, so it's called
the request handler.
2
listen(<port_no>)
Bind the server instance for listening to a particular port.3
Example:
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Simple Web Application Example
1 Import HTTP Module
2 Define Port No.
3 Creating Server Instance
4 Sending HTTP Header
6
Binding Server Instance
with the Port No. 3000
5 Sending Data
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Simple Web Application Example
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Blocking vs Non - Blocking
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Blocking vs Non–Blocking I/O
➢ Blocking is when the execution of additional JavaScript in the Node.js process must wait until a non-JavaScript
operation completes.
➢ "I/O" refers primarily to interaction with the system's disk and network supported by libuv.
More methods will be blocked till
the read method is not executed
More method will execute
asynchronously
(non-blocking way)
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Blocking vs Non–Blocking I/O
Non – Blocking Example:
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Blocking vs Non–Blocking I/O
Blocking Example:
C O N C L U S I O N :
➢ In case of Asynchronous (non-blocking ), once file I/O is complete, it will call the callback function
➢ This allows Node.js to be scalable and process large number of request without worrying about blocking I/O
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Node.js Modules
02
GLOBALS
03
FILE SYSTEM
04
CALLBACKS
05
EVENT
06
HTTP
01
NPM
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
NPM
➢ NPM stands for Node Package Manager
➢ Provides online repositories for node.js packages/modules
➢ Provides command line utility to install Node.js packages along with version management and dependency
management.
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
NPM
npm install Install all the modules as specified in package.json
npm install <Module Name> Install Module using npm
npm install <Module Name> -g Install dependency globally
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Basic Concepts:
02
GLOBALS
03
FILE SYSTEM
04
CALLBACKS
05
EVENT
06
HTTP
01
NPM
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Global Objects
These objects are available in all modules and therefore, are referred as Global Objects
specifies the name of the directory that currently contains the code.
__dirname
specifies the name of the file that currently contains the code.
__filename
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Global Objects
A timer in Node.js is an internal construct that calls a given function after a certain period of time.
setTimeout(callback, delay[, ...args])
➢ Schedules execution of a one-time callback after delay milliseconds
➢ Returns a Timeout for use with clearTimeout( )
setInterval(callback, delay[, ...args])
➢ Schedules repeated execution of callback every delay milliseconds.
➢ Returns a Timeout for use with clearTimeout( )
setImmediate(callback, [,..args])
➢ Schedules an immediate execution of the callback after I/O events' callbacks but
before setTimeout() and setInterval() timers are triggered.
➢ Returns an Immediate for use with clearImmediate().
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Global Objects
output
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Node.js Modules
02
GLOBALS
03
FILE SYSTEM
04
CALLBACKS
05
EVENT
06
HTTP
01
NPM
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
File System
File I/O is provided by simple wrappers around standard POSIX functions. For importing File
System Module (fs), we use: var fs = require("fs");
All methods have asynchronous and synchronous formsImportant Note:
FS Methods
Asynchronous
Forms
Synchronous
Forms
1
var fs = require("fs");
// Asynchronous read
fs.readFile(‘test.txt', function (err, data) { });
// Synchronous read
var data = fs.readFileSync('input.txt');
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
File System
The asynchronous form always takes a completion callback as its last argument.
var fs = require("fs");
// Asynchronous read
fs.readFile(‘test.txt’, function (err, data) {
if (err) {
return console.error(err);
}
console.log(data.toString());
});
FS Methods
Arg 1
Callback Method
(last argument)
Arg 2
Arg 3
Callback As Last Arg.
Important Note: 2
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
File System
First argument in completion callback is always reserved for an exceptionImportant Note: 3
Callback
Method
Arg 1: exception
Arg N
Arg 2
Arg 3
var fs = require("fs");
// Asynchronous read
fs.readFile(‘test.txt’, function (err, data) {
if (err) {
return console.error(err);
}
console.log(data.toString());
});
Reserved for
exception
returns null or undefined
(successful completion)
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Some Methods in File System Module – Open()
fs.open( path, flags[, mode], callback )
fs.openSync( path, flags[, mode] )
fs.close( fd, callback )
➢ var fs = require(‘fs’);
Open File Asynchronously
Open File Synchronously
Closing File
Arguments Description
Path < string > Path of the file
Flags < string > Access Modifiers
Mode < integer > sets the permission and sticky bits, but only if the file was created
Callback < function > Callback signature - function(err, fd)
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Flag Modes in Open Method
Modes Description
r Open file for reading. an exception occurs if the file does not exist
r+ Open file for reading and writing. Exception: if the file does not exist
w Open file for writing. The file is created ( if it does not exist) or truncated (if it exists)
wx Like 'w' but fails if path exists
w+ Open file for reading and writing. File is created (if it does not exist) or truncated (if it exists)
wx+ Same as 'w+' but fails if path exists
a Open file for appending. The file is created if it does not exist
ax Like 'a' but fails if path exists
a+ open file for reading and appending. the file is created if it does not exist.
ax+ Like 'a+' but fails if path exists
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Some Methods in File System Module – Read()
read(fd, buffer, offset, length, position, callback)
readFile(file[, options], callback)
readFileSync(file[, options])
Read Content of a File into Buffer
Reads File Asynchronously
Arguments Description
fd < integer > File Descriptor
buffer <string | Buffer | Unit8Array > The buffer that the data will be written to.
offset < integer > Offset in the buffer to start writing at.
length < integer > Specifies the number of bytes to read.
position < integer > Specifies where to begin reading from in the file
Callback < function > Read Callback signature - function(err, bytesRead, buffer)
Reads File Synchronously
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Some Methods in File System Module – Write()
writeFile(file, data[, options], callback)
writeFileSync(file, data[, options])
Writes into a File Asynchronously
Arguments Description
File Filename or File Descriptor
Data The buffer that the data will be written to.
Options: Encoding, Mode or flag
Callback < function > Callback signature - function(err, bytesRead, buffer)
Writes into a File Synchronously
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Node.js Modules
02
GLOBALS
03
FILE SYSTEM
04
CALLBACKS
05
EVENT
06
HTTP
01
NPM
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Some Methods in File System Module – Write()
Callback is an asynchronous equivalent for a function and is called at the completion of each task
Callback: will execute after
file read is complete
Output:
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Node.js Modules
02
GLOBALS
03
FILE SYSTEM
04
CALLBACKS
05
EVENT
06
HTTP
01
NPM
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Events
➢ Node.js follows event-driven architecture where certain objects (called "emitters") periodically emit
named events which further invokes the listeners (function).
➢ Node.js provide concurrency by using the concept of events and callbacks
➢ All objects that emit events are instances of the EventEmitter class.
var eventEmitter = event.EventEmitter();
Importing ‘events’ module
Object of EventEmitter Class
eventEmitter.emit(‘event’)
Registering listeners
Trigger event
eventEmitter.on(‘event’, eventHandler)
var event = require(‘events’);
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Events
Import Events Module
Creating object of EventEmitter
Emitting event
Registering Listener and defining
event handler
OUTPUT
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Node.js Modules
02
GLOBALS
03
FILE SYSTEM
04
CALLBACKS
05
EVENT
06
HTTP
01
NPM
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Web Application Architecture
Client Client Client Client
Mobile
Browser
Web
Browser
Application
Web Server
Application
Server
File System
Database
External
File System
Client Server Business Layer Data Layer
Makes HTTP Request
to the server
Serve the request
Made by client and
pass the response
contains application
utilized by web server to
do required processing.
Consists of Database
and other data storage
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
HTTP
Import Required Modules
Creating Server
Parse the fetched URL to get pathname
Request file to be read from file system
(index.html)
Creating Header with content type as text or HTML
Generating Response
Listening to port: 3000
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
HTTP
Index.html
Output
Node.js cmd
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Express
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Express Framework
➢ Express is a minimal and flexible Node.js web application framework that provides a robust set of features for
web and mobile applications.
➢ Inspired by Sinatra (web framework based on Ruby) and also, intertwined with Connect, a “plugin” library for
Node.
➢ Express used to come bundled with Connect before version 4.0
➢ After version 4.0, Connect (and all middleware except static) was removed to allow these middleware to be
updated independently
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Express Framework
Importing express module
Retrieve resources
Sending html doc as response
Listening to port: 3000
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
RESTful API
REST stands for REpresentational State Transfer.
Used create a new resource
or update a existing
resource
1 POST
Used to provide a read
only access to a resource
2 GET
Used to create a update
a resource
3 PUT
Used to remove a resource
4 DELETE
C R U DCreate Delete
Read Update
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Popular Frameworks Around
Node.js
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Popular Frameworks Around Node.js
E X P R E S S
Minimalistic Web
Framework based on
Node.js
S A I L S
Project generator,
Middleware and
Templating Engine
M E T E O R
Full Stack Framework
that covers server,
mobile, desktop & web
apps
K O A
Web Framework
designed by the team
behind Express
N O D E
www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING
Demo:
Grocery List Web Application
EDUREKA NODE JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js
Thank You …
Questions/Queries/Feedback

More Related Content

PPTX
NodeJS guide for beginners
PPTX
Introduction Node.js
PDF
Introduction to Node.js
PPTX
Express js
PPTX
Introduction to node.js
PDF
Node JS Crash Course
PPTX
Introduction to Node.js
PDF
NodeJS for Beginner
NodeJS guide for beginners
Introduction Node.js
Introduction to Node.js
Express js
Introduction to node.js
Node JS Crash Course
Introduction to Node.js
NodeJS for Beginner

What's hot (20)

PPTX
Introduction to Node.js
PPT
JavaScript Tutorial
PDF
JavaScript guide 2020 Learn JavaScript
PPTX
Introduction to NodeJS
PPTX
ReactJS presentation.pptx
PDF
ES6 presentation
PDF
Nodejs presentation
PDF
jQuery for beginners
PPTX
Basic Concept of Node.js & NPM
PPTX
Introduction to Node js
PDF
Javascript basics
PDF
Introduction to Spring's Dependency Injection
PDF
Hibernate Presentation
PDF
What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...
PPTX
Namespaces in C#
PDF
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...
PPTX
Java Server Pages(jsp)
PPTX
React JS - A quick introduction tutorial
PDF
Asynchronous JavaScript Programming with Callbacks & Promises
PPTX
Spring data jpa
Introduction to Node.js
JavaScript Tutorial
JavaScript guide 2020 Learn JavaScript
Introduction to NodeJS
ReactJS presentation.pptx
ES6 presentation
Nodejs presentation
jQuery for beginners
Basic Concept of Node.js & NPM
Introduction to Node js
Javascript basics
Introduction to Spring's Dependency Injection
Hibernate Presentation
What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...
Namespaces in C#
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...
Java Server Pages(jsp)
React JS - A quick introduction tutorial
Asynchronous JavaScript Programming with Callbacks & Promises
Spring data jpa
Ad

Similar to Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js Training | Edureka (20)

PDF
Top 50 Node.js Interview Questions and Answers | Edureka
PDF
Communication in Node.js
PDF
Day In A Life Of A Node.js Developer
PDF
Day in a life of a node.js developer
PDF
NodeJS : Communication and Round Robin Way
PDF
Introduction to Node.js
KEY
Playing With Fire - An Introduction to Node.js
PDF
Jaap : node, npm & grunt
PDF
FITC - Node.js 101
PDF
Basic Understanding and Implement of Node.js
PDF
Create Restful Web Application With Node.js Express Framework
PDF
Node.js Course 1 of 2 - Introduction and first steps
PPTX
Introduction to node.js By Ahmed Assaf
PPTX
Introduction to node.js GDD
PDF
Node.js Introduction
PPTX
What is Node.js
PDF
Node.js 101 with Rami Sayar
PDF
Node in Real Time - The Beginning
PDF
Node.js for beginner
PDF
Frontend Track NodeJS
Top 50 Node.js Interview Questions and Answers | Edureka
Communication in Node.js
Day In A Life Of A Node.js Developer
Day in a life of a node.js developer
NodeJS : Communication and Round Robin Way
Introduction to Node.js
Playing With Fire - An Introduction to Node.js
Jaap : node, npm & grunt
FITC - Node.js 101
Basic Understanding and Implement of Node.js
Create Restful Web Application With Node.js Express Framework
Node.js Course 1 of 2 - Introduction and first steps
Introduction to node.js By Ahmed Assaf
Introduction to node.js GDD
Node.js Introduction
What is Node.js
Node.js 101 with Rami Sayar
Node in Real Time - The Beginning
Node.js for beginner
Frontend Track NodeJS
Ad

More from Edureka! (20)

PDF
What to learn during the 21 days Lockdown | Edureka
PDF
Top 10 Dying Programming Languages in 2020 | Edureka
PDF
Top 5 Trending Business Intelligence Tools | Edureka
PDF
Tableau Tutorial for Data Science | Edureka
PDF
Python Programming Tutorial | Edureka
PDF
Top 5 PMP Certifications | Edureka
PDF
Top Maven Interview Questions in 2020 | Edureka
PDF
Linux Mint Tutorial | Edureka
PDF
How to Deploy Java Web App in AWS| Edureka
PDF
Importance of Digital Marketing | Edureka
PDF
RPA in 2020 | Edureka
PDF
Email Notifications in Jenkins | Edureka
PDF
EA Algorithm in Machine Learning | Edureka
PDF
Cognitive AI Tutorial | Edureka
PDF
AWS Cloud Practitioner Tutorial | Edureka
PDF
Blue Prism Top Interview Questions | Edureka
PDF
Big Data on AWS Tutorial | Edureka
PDF
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
PDF
Kubernetes Installation on Ubuntu | Edureka
PDF
Introduction to DevOps | Edureka
What to learn during the 21 days Lockdown | Edureka
Top 10 Dying Programming Languages in 2020 | Edureka
Top 5 Trending Business Intelligence Tools | Edureka
Tableau Tutorial for Data Science | Edureka
Python Programming Tutorial | Edureka
Top 5 PMP Certifications | Edureka
Top Maven Interview Questions in 2020 | Edureka
Linux Mint Tutorial | Edureka
How to Deploy Java Web App in AWS| Edureka
Importance of Digital Marketing | Edureka
RPA in 2020 | Edureka
Email Notifications in Jenkins | Edureka
EA Algorithm in Machine Learning | Edureka
Cognitive AI Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
Blue Prism Top Interview Questions | Edureka
Big Data on AWS Tutorial | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Kubernetes Installation on Ubuntu | Edureka
Introduction to DevOps | Edureka

Recently uploaded (20)

PPT
What is a Computer? Input Devices /output devices
PPTX
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
CloudStack 4.21: First Look Webinar slides
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Comparative analysis of machine learning models for fake news detection in so...
PPT
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
PDF
The influence of sentiment analysis in enhancing early warning system model f...
PDF
Flame analysis and combustion estimation using large language and vision assi...
PDF
Architecture types and enterprise applications.pdf
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
A proposed approach for plagiarism detection in Myanmar Unicode text
PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PDF
Consumable AI The What, Why & How for Small Teams.pdf
PDF
Enhancing plagiarism detection using data pre-processing and machine learning...
PDF
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
What is a Computer? Input Devices /output devices
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
A contest of sentiment analysis: k-nearest neighbor versus neural network
Developing a website for English-speaking practice to English as a foreign la...
CloudStack 4.21: First Look Webinar slides
A review of recent deep learning applications in wood surface defect identifi...
1 - Historical Antecedents, Social Consideration.pdf
Comparative analysis of machine learning models for fake news detection in so...
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
The influence of sentiment analysis in enhancing early warning system model f...
Flame analysis and combustion estimation using large language and vision assi...
Architecture types and enterprise applications.pdf
NewMind AI Weekly Chronicles – August ’25 Week III
A proposed approach for plagiarism detection in Myanmar Unicode text
Custom Battery Pack Design Considerations for Performance and Safety
Credit Without Borders: AI and Financial Inclusion in Bangladesh
Consumable AI The What, Why & How for Small Teams.pdf
Enhancing plagiarism detection using data pre-processing and machine learning...
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide

Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js Training | Edureka

  • 1. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Webpage
  • 2. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Agenda ❖ Client Server Architecture ❖ Limitations of Multi–Threaded Model ❖ What is Node.js? ❖ Features of Node.js ❖ Node.js Installation ❖ Blocking Vs. Non – Blocking I/O ❖ Creating Node.js First Program ❖ Node.js Modules ❖ Demo – Grocery List Web Application using Node.js
  • 3. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Client – Server Architecture
  • 4. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Client Server Architecture Client 1 Client 2 Client 3 Server Database
  • 5. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Traditional Multi-Threaded Model Handle Request A Handle Request B Handle Request C Read ResponseRead Request Assign Thread for A Assign Thread for C Assign Thread for B Thread Thread Thread User HTTP Request A User HTTP Request B User HTTP Request C User HTTP Request D
  • 6. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Limitations of Multi – Threaded Approach
  • 7. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Limitations of Multi-Threaded Models Shared Resources Updating Resources Wants to Update Wants to Update Thread A Thread C Thread B ➢ In a multi-threaded HTTP server, for each and every request that the server receives, it creates a separate thread which handles that request ➢ If a request acquires a lock in the shared resource and it is ‘exclusive’, it will affect result of other requests
  • 8. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING High chances of deadlock if application is not designed properly Limitations of Multi-Threaded Models Web Servers need to handle thousands of HTTP requests. So, many threads may have to wait for network operations For thousands of simultaneous requests, spawning threads for all processes would not achieve the desired scalability Scalability Complex Deadlock
  • 9. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING What is Node.js ?
  • 10. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING What is Node.js? ➢Node.js is an open source runtime environment for server-side and networking applications and is single threaded. ➢Uses Google JavaScript V8 Engine to execute code. ➢It is cross platform environment and can run on OS X, Microsoft Windows, Linux, FreeBSD, NonStop and IBM. ➢Provides an event driven architecture and non blocking I/O that is optimized and scalable. N O D E
  • 11. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Features of Node.js
  • 12. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Features of Node.js 1. Asynchronous Caller Recipient request response callback ➢ When request is made to server, instead of waiting for the request to complete, server continues to process other requests ➢ When request processing completes, the response is sent to caller using callback mechanism Asynchronous Model Asynchronous Event Driven Very Fast Scalable
  • 13. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Features of Node.js ➢ When request is made to server, instead of waiting for the request to complete, server continues to process other requests ➢ When request processing completes, the response is sent to caller using callback mechanism Event Emitters Event Loop (Single - threaded) File System Network Process Other Event Queue Thread Pool 2. Single Threaded and Event Driven: Asynchronous Event Driven Very Fast Scalable
  • 14. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Features of Node.js 3. Very Fast 4. Single Threaded but Highly Scalable Being built on Google Chrome's V8 JavaScript Engine, Node.js library is very fast in code execution. Node.js is highly scalable because event mechanism helps the server to respond in a non- blocking way. Asynchronous Event Driven Very Fast Scalable
  • 15. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Node.js Installation
  • 16. EDUREKA NODE JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js Node.js Installation Go to https://nodejs.org/en/1 Download and Install2
  • 17. EDUREKA NODE JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js Node.js Installation Open node.js command prompt3 ➢ node –version Check the version of Node.js installed
  • 18. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Creating Your First Node.js Program
  • 19. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Node.js First Program Create a Directory: mkdir node_example1 Create a File: first_example.js2 Go to the node_example directory3 Execute the java script file: node first_example.js4 2 1 3 4
  • 20. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Simple Web Application Example
  • 21. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Simple Web Application Example Example: Importing Module: ‘require’ is used to load a Node.js modules. 1 Example: Creating Server ➢ Used to create web server object ➢ Function (request, response) is called once for every HTTP request to the server, so it's called the request handler. 2 listen(<port_no>) Bind the server instance for listening to a particular port.3 Example:
  • 22. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Simple Web Application Example 1 Import HTTP Module 2 Define Port No. 3 Creating Server Instance 4 Sending HTTP Header 6 Binding Server Instance with the Port No. 3000 5 Sending Data
  • 23. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Simple Web Application Example
  • 24. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Blocking vs Non - Blocking
  • 25. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Blocking vs Non–Blocking I/O ➢ Blocking is when the execution of additional JavaScript in the Node.js process must wait until a non-JavaScript operation completes. ➢ "I/O" refers primarily to interaction with the system's disk and network supported by libuv. More methods will be blocked till the read method is not executed More method will execute asynchronously (non-blocking way)
  • 26. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Blocking vs Non–Blocking I/O Non – Blocking Example:
  • 27. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Blocking vs Non–Blocking I/O Blocking Example: C O N C L U S I O N : ➢ In case of Asynchronous (non-blocking ), once file I/O is complete, it will call the callback function ➢ This allows Node.js to be scalable and process large number of request without worrying about blocking I/O
  • 28. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Node.js Modules 02 GLOBALS 03 FILE SYSTEM 04 CALLBACKS 05 EVENT 06 HTTP 01 NPM
  • 29. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING NPM ➢ NPM stands for Node Package Manager ➢ Provides online repositories for node.js packages/modules ➢ Provides command line utility to install Node.js packages along with version management and dependency management.
  • 30. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING NPM npm install Install all the modules as specified in package.json npm install <Module Name> Install Module using npm npm install <Module Name> -g Install dependency globally
  • 31. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Basic Concepts: 02 GLOBALS 03 FILE SYSTEM 04 CALLBACKS 05 EVENT 06 HTTP 01 NPM
  • 32. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Global Objects These objects are available in all modules and therefore, are referred as Global Objects specifies the name of the directory that currently contains the code. __dirname specifies the name of the file that currently contains the code. __filename
  • 33. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Global Objects A timer in Node.js is an internal construct that calls a given function after a certain period of time. setTimeout(callback, delay[, ...args]) ➢ Schedules execution of a one-time callback after delay milliseconds ➢ Returns a Timeout for use with clearTimeout( ) setInterval(callback, delay[, ...args]) ➢ Schedules repeated execution of callback every delay milliseconds. ➢ Returns a Timeout for use with clearTimeout( ) setImmediate(callback, [,..args]) ➢ Schedules an immediate execution of the callback after I/O events' callbacks but before setTimeout() and setInterval() timers are triggered. ➢ Returns an Immediate for use with clearImmediate().
  • 34. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Global Objects output
  • 35. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Node.js Modules 02 GLOBALS 03 FILE SYSTEM 04 CALLBACKS 05 EVENT 06 HTTP 01 NPM
  • 36. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING File System File I/O is provided by simple wrappers around standard POSIX functions. For importing File System Module (fs), we use: var fs = require("fs"); All methods have asynchronous and synchronous formsImportant Note: FS Methods Asynchronous Forms Synchronous Forms 1 var fs = require("fs"); // Asynchronous read fs.readFile(‘test.txt', function (err, data) { }); // Synchronous read var data = fs.readFileSync('input.txt');
  • 37. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING File System The asynchronous form always takes a completion callback as its last argument. var fs = require("fs"); // Asynchronous read fs.readFile(‘test.txt’, function (err, data) { if (err) { return console.error(err); } console.log(data.toString()); }); FS Methods Arg 1 Callback Method (last argument) Arg 2 Arg 3 Callback As Last Arg. Important Note: 2
  • 38. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING File System First argument in completion callback is always reserved for an exceptionImportant Note: 3 Callback Method Arg 1: exception Arg N Arg 2 Arg 3 var fs = require("fs"); // Asynchronous read fs.readFile(‘test.txt’, function (err, data) { if (err) { return console.error(err); } console.log(data.toString()); }); Reserved for exception returns null or undefined (successful completion)
  • 39. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Some Methods in File System Module – Open() fs.open( path, flags[, mode], callback ) fs.openSync( path, flags[, mode] ) fs.close( fd, callback ) ➢ var fs = require(‘fs’); Open File Asynchronously Open File Synchronously Closing File Arguments Description Path < string > Path of the file Flags < string > Access Modifiers Mode < integer > sets the permission and sticky bits, but only if the file was created Callback < function > Callback signature - function(err, fd)
  • 40. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Flag Modes in Open Method Modes Description r Open file for reading. an exception occurs if the file does not exist r+ Open file for reading and writing. Exception: if the file does not exist w Open file for writing. The file is created ( if it does not exist) or truncated (if it exists) wx Like 'w' but fails if path exists w+ Open file for reading and writing. File is created (if it does not exist) or truncated (if it exists) wx+ Same as 'w+' but fails if path exists a Open file for appending. The file is created if it does not exist ax Like 'a' but fails if path exists a+ open file for reading and appending. the file is created if it does not exist. ax+ Like 'a+' but fails if path exists
  • 41. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Some Methods in File System Module – Read() read(fd, buffer, offset, length, position, callback) readFile(file[, options], callback) readFileSync(file[, options]) Read Content of a File into Buffer Reads File Asynchronously Arguments Description fd < integer > File Descriptor buffer <string | Buffer | Unit8Array > The buffer that the data will be written to. offset < integer > Offset in the buffer to start writing at. length < integer > Specifies the number of bytes to read. position < integer > Specifies where to begin reading from in the file Callback < function > Read Callback signature - function(err, bytesRead, buffer) Reads File Synchronously
  • 42. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Some Methods in File System Module – Write() writeFile(file, data[, options], callback) writeFileSync(file, data[, options]) Writes into a File Asynchronously Arguments Description File Filename or File Descriptor Data The buffer that the data will be written to. Options: Encoding, Mode or flag Callback < function > Callback signature - function(err, bytesRead, buffer) Writes into a File Synchronously
  • 43. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Node.js Modules 02 GLOBALS 03 FILE SYSTEM 04 CALLBACKS 05 EVENT 06 HTTP 01 NPM
  • 44. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Some Methods in File System Module – Write() Callback is an asynchronous equivalent for a function and is called at the completion of each task Callback: will execute after file read is complete Output:
  • 45. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Node.js Modules 02 GLOBALS 03 FILE SYSTEM 04 CALLBACKS 05 EVENT 06 HTTP 01 NPM
  • 46. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Events ➢ Node.js follows event-driven architecture where certain objects (called "emitters") periodically emit named events which further invokes the listeners (function). ➢ Node.js provide concurrency by using the concept of events and callbacks ➢ All objects that emit events are instances of the EventEmitter class. var eventEmitter = event.EventEmitter(); Importing ‘events’ module Object of EventEmitter Class eventEmitter.emit(‘event’) Registering listeners Trigger event eventEmitter.on(‘event’, eventHandler) var event = require(‘events’);
  • 47. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Events Import Events Module Creating object of EventEmitter Emitting event Registering Listener and defining event handler OUTPUT
  • 48. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Node.js Modules 02 GLOBALS 03 FILE SYSTEM 04 CALLBACKS 05 EVENT 06 HTTP 01 NPM
  • 49. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Web Application Architecture Client Client Client Client Mobile Browser Web Browser Application Web Server Application Server File System Database External File System Client Server Business Layer Data Layer Makes HTTP Request to the server Serve the request Made by client and pass the response contains application utilized by web server to do required processing. Consists of Database and other data storage
  • 50. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING HTTP Import Required Modules Creating Server Parse the fetched URL to get pathname Request file to be read from file system (index.html) Creating Header with content type as text or HTML Generating Response Listening to port: 3000
  • 51. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING HTTP Index.html Output Node.js cmd
  • 52. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Express
  • 53. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Express Framework ➢ Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. ➢ Inspired by Sinatra (web framework based on Ruby) and also, intertwined with Connect, a “plugin” library for Node. ➢ Express used to come bundled with Connect before version 4.0 ➢ After version 4.0, Connect (and all middleware except static) was removed to allow these middleware to be updated independently
  • 54. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Express Framework Importing express module Retrieve resources Sending html doc as response Listening to port: 3000
  • 55. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING RESTful API REST stands for REpresentational State Transfer. Used create a new resource or update a existing resource 1 POST Used to provide a read only access to a resource 2 GET Used to create a update a resource 3 PUT Used to remove a resource 4 DELETE C R U DCreate Delete Read Update
  • 56. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Popular Frameworks Around Node.js
  • 57. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Popular Frameworks Around Node.js E X P R E S S Minimalistic Web Framework based on Node.js S A I L S Project generator, Middleware and Templating Engine M E T E O R Full Stack Framework that covers server, mobile, desktop & web apps K O A Web Framework designed by the team behind Express N O D E
  • 58. www.edureka.co/mastering-node-jsEDUREKA NODE JS CERTIFICATION TRAINING Demo: Grocery List Web Application
  • 59. EDUREKA NODE JS CERTIFICATION TRAINING www.edureka.co/mastering-node-js Thank You … Questions/Queries/Feedback