SlideShare a Scribd company logo
Node JS Introduction
Parth Joshi
Entrepreneur | Techno - Consultant | Corporate Trainer
parthjoshi.in | LinkedIN | Twitter |
The need for node.js like
architecture
Evolution of Web Server and client
architecture
Client Server
HTTP Request
Response
(Static Documents)
Web Client (Browser):
Javascript
HTML
Evolution of Web Server and client
architecture
Client Server
Web Client (Browser):
Javascript
HTML
Web Servers:
Java
Python
PHP
Ruby
.NET
HTTP Request
Response
(Static Documents,
Dynamic Web Pages)
Evolution of Web Server and client
architecture
Client Server
Web Client (Browser):
Javascript
HTML
Mobile Client
Android
iOS
Native Clients
Java
Python
API Servers:
Java
Python
PHP
Ruby
.NET
HTTP Request
Response
Dynamic Pages
Static Documents
XML Response
Evolution of Web Server and client
architecture
Client Server
Web Client (Browser):
Javascript
HTML
Mobile Client
Android
iOS
Native Clients
Java
Python
API Servers:
Java
Python
Node
PHP
Ruby
.NET
HTTP Request
JSON Response
[Employee list]
{
"employees": [
{
"firstName": "John",
"lastName": "Doe"
},
{ ...
Enter JSON
JSON VS. XML
JSON file XML File
Evolution of Web Server and client
architecture
How is JSON Used
Client Server
Web Client (Browser):
Javascript
HTML
Mobile Client
Android
iOS
Native Clients
Java
Python
API Servers:
Java
Python
Node
PHP
Ruby
.NET
HTTP Request
JSON Response
[Employee list]
{
"employees": [
{
"firstName": "John",
"lastName": "Doe"
},
{ ...
Evolution of Web Server and client
architecture
The Idea behind Mongo DB
Client Server
Web Client (Browser):
Javascript
HTML
Mobile Client
Android
iOS
Native Clients
Java
Python
API Servers:
Java
Python
Node
PHP
Ruby
.NET
HTTP Request
JSON Response
[Employee list]
{
"employees": [
{
"firstName": "John",
"lastName": "Doe"
},
{ ...
Store Data in form of JSON Objects
So what is nodejs?
Chrome V8 Engine
• The V8 JavaScript Engine is an open source JavaScript engine
• developed by The Chromium Project for the Google Chrome web
browser
Chrome V8 Engine
Chrome V8 Engine
Javascript Code
Native Machine Code
• V8 compiles JavaScript to native machine code before
executing it.
• instead of more traditional techniques such as interpreting
bytecode or compiling the whole program to machine code
and executing it from a filesystem.
• The compiled code is additionally optimized (and re-optimized)
dynamically at runtime, based on heuristics of the code's
execution profile.
• Optimization techniques used include inlining, elision of
expensive runtime properties, and inline caching, among many
others.
Written in c/c++
So What’s Nodejs?
Node Js runs on Chrome V8 Engine
In Simple words:
“Node.Js is server side javascript framework”
So What’s Nodejs?
Node Js runs on Chrome V8 Engine
In not-so-simple Words :
“Node.js is a high-performance network applications framework,
well optimized for high concurrent environments.”
Highlights on node.js
• Node.js uses an event-driven, non-blocking I/O model, which makes
it lightweight.
• It makes use of event-loops via JavaScript’s callback functionality to
implement the non-blocking I/O.
• Programs for Node.js are written in JavaScript but not in the same
JavaScript we are use to. There is no DOM implementation provided
by Node.js, i.e. you can not do this:
• var element = document.getElementById(“elementId”);
• Everything inside Node.js runs in a single-thread.
Node.js stack
Lets get some hands on.
• Install node.js
• Install Visual Studio Code.
A Simple Hello world example
console.log ("Hello world with node");
Hello.js
Execute the code:
$ node Hello.js
Hello world with node
$ _
Let take some command line arguments
Use process object to access the command line arguments
Example:
process.argv[i] : to access the command line arguments
process.argv[i].length: to get the number of arguments
Print Command line arguments
var argument = "";
for (var i = 0; i < process.argv.length; i++) {
argument = process.argv[i];
console.log(argument);
}
File: comline.js
$node comline.js hello world
node
comline.js
hello
world
$node comline.js
node
comline.js
$node comline.js hello world
node
comline.js
hello
world
Execution:
Lets run a server now
// getting the http module
var http = require('http');
// creating server instance
var server = http.createServer(function(request, response){
response.writeHead(200, {'Content-Type' : 'text/html'});
response.write('Hello world<br/>');
response.end('bye bye');
});
// listening to server at port 9090
server.listen(9090,'127.0.0.1');
Understanding basics
Import node module using require function.
var http = require('http');
Understanding basics
Create a server instance
// creating server instance
var server = http.createServer(function(request, response){
response.writeHead(200, {'Content-Type' : 'text/html'});
response.write('Hello world <br/>');
response.end('bye bye');
});
As per the docs:
http.createServer([requestListener])
Returns a new instance of http.Server.
The requestListener is a function which is automatically added to the 'request' event.
Understanding basics
Listen to port 9090 at localhost
server.listen(9090,'127.0.0.1');
Coming back to Event Loop
Lets have a feel of non blocking IO
var fs = require('fs');
var data = fs.readFileSync("myText.txt");
var fs = require('fs');
fs.readFile('myText.txt',function(err,data){
…
});
Synchronous IO Async IO (Non Blocking)
File Streams
Input Streams
var fs = require('fs');
// creating a reading stream...
var readerStream = fs.createReadStream ("mytext.txt");
var dataRead = "";
readerStream.setEncoding('utf8');
File Streams
Events for Streams
data - This event is fired when there is data is available to read.
end - This event is fired when there is no more data 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
File Streams
Input Streams
readerStream.on ('data', function (buffer){
dataRead += buffer;
}) ;
// when there is end in the stream
readerStream.on ('end', function (){
console.log(dataRead);
}) ;
// if there is error in the stream connection
readerStream.on ('error', function (err){
console.log("There is error:"+err);
}) ;
File Streams
var fs = require("fs");
var data = 'Simply Easy Learning';
// Create a writable stream
var writerStream = fs.createWriteStream('output.txt');
// Write the data to stream with encoding to be utf8
writerStream.write(data,'UTF8');
// Mark the end of file
writerStream.end();
Input Streams
Thank you
Parth Joshi
Entrepreneur | Techno - Consultant | Corporate Trainer
parthjoshi.in | LinkedIN | Twitter | joshiparthin@gmail.com

More Related Content

What's hot (20)

PDF
Node Security: The Good, Bad & Ugly
Bishan Singh
 
PPT
Building your first Node app with Connect & Express
Christian Joudrey
 
PDF
Node intro
Vishal Sharma
 
PPTX
Alexey Kupriyanenko "The State of Modern JavaScript and Web in 2020 - Real us...
Fwdays
 
PPTX
Building Your First App with MongoDB
MongoDB
 
PDF
An Introduction to Tornado
Gavin Roy
 
PDF
Node.js & Twitter Bootstrap Crash Course
Aaron Silverman
 
PPTX
introduction to node.js
orkaplan
 
PDF
Node js first look - 2016
Yauheni Nikanovich
 
PDF
Introduction to Nodejs
Gabriele Lana
 
PDF
Flask jwt authentication tutorial
Katy Slemon
 
PDF
Analyse Yourself
Norberto Leite
 
PDF
Instant and offline apps with Service Worker
Chang W. Doh
 
PDF
Node.js first slide
Soni Pandey
 
PDF
Real-time Web Application with Socket.IO, Node.js, and Redis
York Tsai
 
PDF
"The little big project. From zero to hero in two weeks with 3 front-end engi...
Fwdays
 
PDF
JavaScript Web Workers
Tobias Pfeiffer
 
PDF
Modern UI Development With Node.js
Ryan Anklam
 
PDF
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
andreaslubbe
 
PDF
Node azure
Emanuele DelBono
 
Node Security: The Good, Bad & Ugly
Bishan Singh
 
Building your first Node app with Connect & Express
Christian Joudrey
 
Node intro
Vishal Sharma
 
Alexey Kupriyanenko "The State of Modern JavaScript and Web in 2020 - Real us...
Fwdays
 
Building Your First App with MongoDB
MongoDB
 
An Introduction to Tornado
Gavin Roy
 
Node.js & Twitter Bootstrap Crash Course
Aaron Silverman
 
introduction to node.js
orkaplan
 
Node js first look - 2016
Yauheni Nikanovich
 
Introduction to Nodejs
Gabriele Lana
 
Flask jwt authentication tutorial
Katy Slemon
 
Analyse Yourself
Norberto Leite
 
Instant and offline apps with Service Worker
Chang W. Doh
 
Node.js first slide
Soni Pandey
 
Real-time Web Application with Socket.IO, Node.js, and Redis
York Tsai
 
"The little big project. From zero to hero in two weeks with 3 front-end engi...
Fwdays
 
JavaScript Web Workers
Tobias Pfeiffer
 
Modern UI Development With Node.js
Ryan Anklam
 
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
andreaslubbe
 
Node azure
Emanuele DelBono
 

Similar to Node.js introduction (20)

PPTX
Nodejs
Vinod Kumar Marupu
 
PPTX
Node.js Workshop - Sela SDP 2015
Nir Noy
 
PPTX
Introduction to Node.js
Vikash Singh
 
PPTX
Proposal
Constantine Priemski
 
PPTX
Node
Ankit Chawla
 
PPTX
Scalable network applications, event-driven - Node JS
Cosmin Mereuta
 
PPT
Node
Manav Prasad
 
ODP
Introduce about Nodejs - duyetdev.com
Van-Duyet Le
 
KEY
Node.js - A practical introduction (v2)
Felix Geisendörfer
 
PPT
Node.js
Neethu Dasan
 
PDF
Server Side Event Driven Programming
Kamal Hussain
 
PPTX
An overview of node.js
valuebound
 
PPT
New kid on the block node.js
Joel Divekar
 
PPT
18_Node.js.ppt
KhalilSalhi7
 
PPTX
What is Node.js
mohamed hadrich
 
PPTX
Kalp Corporate Node JS Perfect Guide
Kalp Corporate
 
PDF
Tech io nodejs_20130531_v0.6
Ganesh Kondal
 
PPTX
Intro to node and non blocking io
Amy Hua
 
PPTX
NodeJS - Server Side JS
Ganesh Kondal
 
Node.js Workshop - Sela SDP 2015
Nir Noy
 
Introduction to Node.js
Vikash Singh
 
Scalable network applications, event-driven - Node JS
Cosmin Mereuta
 
Introduce about Nodejs - duyetdev.com
Van-Duyet Le
 
Node.js - A practical introduction (v2)
Felix Geisendörfer
 
Node.js
Neethu Dasan
 
Server Side Event Driven Programming
Kamal Hussain
 
An overview of node.js
valuebound
 
New kid on the block node.js
Joel Divekar
 
18_Node.js.ppt
KhalilSalhi7
 
What is Node.js
mohamed hadrich
 
Kalp Corporate Node JS Perfect Guide
Kalp Corporate
 
Tech io nodejs_20130531_v0.6
Ganesh Kondal
 
Intro to node and non blocking io
Amy Hua
 
NodeJS - Server Side JS
Ganesh Kondal
 
Ad

Recently uploaded (20)

PPT
introduction to networking with basics coverage
RamananMuthukrishnan
 
PPTX
Softuni - Psychology of entrepreneurship
Kalin Karakehayov
 
PPTX
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
PPTX
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
PPTX
Orchestrating things in Angular application
Peter Abraham
 
DOCX
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
PPTX
internet básico presentacion es una red global
70965857
 
PPTX
Optimization_Techniques_ML_Presentation.pptx
farispalayi
 
PDF
The Internet - By the numbers, presented at npNOG 11
APNIC
 
PDF
Build Fast, Scale Faster: Milvus vs. Zilliz Cloud for Production-Ready AI
Zilliz
 
PPTX
一比一原版(SUNY-Albany毕业证)纽约州立大学奥尔巴尼分校毕业证如何办理
Taqyea
 
PDF
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
PPTX
原版西班牙莱昂大学毕业证(León毕业证书)如何办理
Taqyea
 
PPTX
ONLINE BIRTH CERTIFICATE APPLICATION SYSYTEM PPT.pptx
ShyamasreeDutta
 
PDF
BRKACI-1003 ACI Brownfield Migration - Real World Experiences and Best Practi...
fcesargonca
 
PDF
Cleaning up your RPKI invalids, presented at PacNOG 35
APNIC
 
PPTX
PM200.pptxghjgfhjghjghjghjghjghjghjghjghjghj
breadpaan921
 
PPTX
sajflsajfljsdfljslfjslfsdfas;fdsfksadfjlsdflkjslgfs;lfjlsajfl;sajfasfd.pptx
theknightme
 
PDF
BRKACI-1001 - Your First 7 Days of ACI.pdf
fcesargonca
 
PPT
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
introduction to networking with basics coverage
RamananMuthukrishnan
 
Softuni - Psychology of entrepreneurship
Kalin Karakehayov
 
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
Orchestrating things in Angular application
Peter Abraham
 
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
internet básico presentacion es una red global
70965857
 
Optimization_Techniques_ML_Presentation.pptx
farispalayi
 
The Internet - By the numbers, presented at npNOG 11
APNIC
 
Build Fast, Scale Faster: Milvus vs. Zilliz Cloud for Production-Ready AI
Zilliz
 
一比一原版(SUNY-Albany毕业证)纽约州立大学奥尔巴尼分校毕业证如何办理
Taqyea
 
Azure_DevOps introduction for CI/CD and Agile
henrymails
 
原版西班牙莱昂大学毕业证(León毕业证书)如何办理
Taqyea
 
ONLINE BIRTH CERTIFICATE APPLICATION SYSYTEM PPT.pptx
ShyamasreeDutta
 
BRKACI-1003 ACI Brownfield Migration - Real World Experiences and Best Practi...
fcesargonca
 
Cleaning up your RPKI invalids, presented at PacNOG 35
APNIC
 
PM200.pptxghjgfhjghjghjghjghjghjghjghjghjghj
breadpaan921
 
sajflsajfljsdfljslfjslfsdfas;fdsfksadfjlsdflkjslgfs;lfjlsajfl;sajfasfd.pptx
theknightme
 
BRKACI-1001 - Your First 7 Days of ACI.pdf
fcesargonca
 
Agilent Optoelectronic Solutions for Mobile Application
andreashenniger2
 
Ad

Node.js introduction

  • 1. Node JS Introduction Parth Joshi Entrepreneur | Techno - Consultant | Corporate Trainer parthjoshi.in | LinkedIN | Twitter |
  • 2. The need for node.js like architecture
  • 3. Evolution of Web Server and client architecture Client Server HTTP Request Response (Static Documents) Web Client (Browser): Javascript HTML
  • 4. Evolution of Web Server and client architecture Client Server Web Client (Browser): Javascript HTML Web Servers: Java Python PHP Ruby .NET HTTP Request Response (Static Documents, Dynamic Web Pages)
  • 5. Evolution of Web Server and client architecture Client Server Web Client (Browser): Javascript HTML Mobile Client Android iOS Native Clients Java Python API Servers: Java Python PHP Ruby .NET HTTP Request Response Dynamic Pages Static Documents XML Response
  • 6. Evolution of Web Server and client architecture Client Server Web Client (Browser): Javascript HTML Mobile Client Android iOS Native Clients Java Python API Servers: Java Python Node PHP Ruby .NET HTTP Request JSON Response [Employee list] { "employees": [ { "firstName": "John", "lastName": "Doe" }, { ...
  • 7. Enter JSON JSON VS. XML JSON file XML File
  • 8. Evolution of Web Server and client architecture How is JSON Used Client Server Web Client (Browser): Javascript HTML Mobile Client Android iOS Native Clients Java Python API Servers: Java Python Node PHP Ruby .NET HTTP Request JSON Response [Employee list] { "employees": [ { "firstName": "John", "lastName": "Doe" }, { ...
  • 9. Evolution of Web Server and client architecture The Idea behind Mongo DB Client Server Web Client (Browser): Javascript HTML Mobile Client Android iOS Native Clients Java Python API Servers: Java Python Node PHP Ruby .NET HTTP Request JSON Response [Employee list] { "employees": [ { "firstName": "John", "lastName": "Doe" }, { ... Store Data in form of JSON Objects
  • 10. So what is nodejs?
  • 11. Chrome V8 Engine • The V8 JavaScript Engine is an open source JavaScript engine • developed by The Chromium Project for the Google Chrome web browser
  • 12. Chrome V8 Engine Chrome V8 Engine Javascript Code Native Machine Code • V8 compiles JavaScript to native machine code before executing it. • instead of more traditional techniques such as interpreting bytecode or compiling the whole program to machine code and executing it from a filesystem. • The compiled code is additionally optimized (and re-optimized) dynamically at runtime, based on heuristics of the code's execution profile. • Optimization techniques used include inlining, elision of expensive runtime properties, and inline caching, among many others. Written in c/c++
  • 13. So What’s Nodejs? Node Js runs on Chrome V8 Engine In Simple words: “Node.Js is server side javascript framework”
  • 14. So What’s Nodejs? Node Js runs on Chrome V8 Engine In not-so-simple Words : “Node.js is a high-performance network applications framework, well optimized for high concurrent environments.”
  • 15. Highlights on node.js • Node.js uses an event-driven, non-blocking I/O model, which makes it lightweight. • It makes use of event-loops via JavaScript’s callback functionality to implement the non-blocking I/O. • Programs for Node.js are written in JavaScript but not in the same JavaScript we are use to. There is no DOM implementation provided by Node.js, i.e. you can not do this: • var element = document.getElementById(“elementId”); • Everything inside Node.js runs in a single-thread.
  • 17. Lets get some hands on. • Install node.js • Install Visual Studio Code.
  • 18. A Simple Hello world example console.log ("Hello world with node"); Hello.js Execute the code: $ node Hello.js Hello world with node $ _
  • 19. Let take some command line arguments Use process object to access the command line arguments Example: process.argv[i] : to access the command line arguments process.argv[i].length: to get the number of arguments
  • 20. Print Command line arguments var argument = ""; for (var i = 0; i < process.argv.length; i++) { argument = process.argv[i]; console.log(argument); } File: comline.js $node comline.js hello world node comline.js hello world $node comline.js node comline.js $node comline.js hello world node comline.js hello world Execution:
  • 21. Lets run a server now // getting the http module var http = require('http'); // creating server instance var server = http.createServer(function(request, response){ response.writeHead(200, {'Content-Type' : 'text/html'}); response.write('Hello world<br/>'); response.end('bye bye'); }); // listening to server at port 9090 server.listen(9090,'127.0.0.1');
  • 22. Understanding basics Import node module using require function. var http = require('http');
  • 23. Understanding basics Create a server instance // creating server instance var server = http.createServer(function(request, response){ response.writeHead(200, {'Content-Type' : 'text/html'}); response.write('Hello world <br/>'); response.end('bye bye'); }); As per the docs: http.createServer([requestListener]) Returns a new instance of http.Server. The requestListener is a function which is automatically added to the 'request' event.
  • 24. Understanding basics Listen to port 9090 at localhost server.listen(9090,'127.0.0.1');
  • 25. Coming back to Event Loop
  • 26. Lets have a feel of non blocking IO var fs = require('fs'); var data = fs.readFileSync("myText.txt"); var fs = require('fs'); fs.readFile('myText.txt',function(err,data){ … }); Synchronous IO Async IO (Non Blocking)
  • 27. File Streams Input Streams var fs = require('fs'); // creating a reading stream... var readerStream = fs.createReadStream ("mytext.txt"); var dataRead = ""; readerStream.setEncoding('utf8');
  • 28. File Streams Events for Streams data - This event is fired when there is data is available to read. end - This event is fired when there is no more data 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
  • 29. File Streams Input Streams readerStream.on ('data', function (buffer){ dataRead += buffer; }) ; // when there is end in the stream readerStream.on ('end', function (){ console.log(dataRead); }) ; // if there is error in the stream connection readerStream.on ('error', function (err){ console.log("There is error:"+err); }) ;
  • 30. File Streams var fs = require("fs"); var data = 'Simply Easy Learning'; // Create a writable stream var writerStream = fs.createWriteStream('output.txt'); // Write the data to stream with encoding to be utf8 writerStream.write(data,'UTF8'); // Mark the end of file writerStream.end(); Input Streams
  • 31. Thank you Parth Joshi Entrepreneur | Techno - Consultant | Corporate Trainer parthjoshi.in | LinkedIN | Twitter | [email protected]