SlideShare una empresa de Scribd logo
1 de 15
Descargar para leer sin conexión
Node.js
That's what happens when JavaScript runs on Server too
By Swapnil Mishra
But Javascript on server ?
• Powerful JavaScript engine which powers Google Chrome browser
and Chrome OS.
• Can also be considered as a Virtual Machine.
• Node.js runs on the top of V8.
• Utilizes the networking and parsing features which are done elegantly
in V8.
V8 made it possible
• There are differences in the JavaScript which runs in browser vs
node.js.
e.g. There are no "window" or "document" objects.
• Things which have been taken from V8 are asynchronous/non-
blocking nature of JavaScript, Syntax, Data Structures(most of them).
Which means everyones favorite closure/anonymous functions will
work here too.
• Things which have been added into it are Timers, Processes, Events,
Buffers, Streams, Crypto, File Systems, HTTP, HTTPS and many more.
Wait, so you are saying this node.js is same
as the javascript which runs in browser ?
• Model is evented rather than threaded.
• Using an event loop, you ask it to do something and it will get back to
you when its done.(we will see an example later)
• Conventional languages are run in threaded environment where they
create threads and just loop/pool in to wait for the result. Thats not
the case with node.js.
• Because of the evented nature it can perform Non-Blocking Network
I/O.
Oh cool tell me more about it !!
Example of Blocking I/O
public INeedData {
public Data getMeSomeData(queryParam){
Data toBeReturned = db.query(queryParam);
iAmBlockedTillQueryExecutes();
return toBeReturned;
Blocking I/O
}
}
// get data performs blocking I/O call because of which
// iAmBlockedTillQueryExecutes() is blocked
Why should i care about Non-Blocking I/O
?
Think about what we really do on web server.
• Grab Files
• Call Databases
• Pull from caches
• Wait on other connections
It's all I/O and we know that I/O latency of
network is highest.
Why should i care about Non-Blocking I/O
?
• Totally and so does web-servers like Apache, IIS.
• But threading is not free.
• There are context switches and executions stacks take
memory.
What should we do then ?
Hey but that's exactly why i use thread
Use a single thread. Do little pieces of work and do them quickly.
But single thread !!!
We have been using multithreading from ages
when UI used to be in "Java Swing" !!!
Are you crazy ?
Can it handle ?
Event loop comes to the rescue
Apache vs NGINX
Apache vs NGINX
The difference?
• Apache uses one thread per connection.
• NGINX does not use threads. It uses an event loop.
So basically we have to change our programming model to something
where we could easily write non-blocking code.
public INeedData {
public Data getMeSomeData(queryParam){
Data toBeReturned = db.query(queryParam,function(){
return
toBeReturned;});
iAmNotBlockedTillQueryExecutes();
}
}
Apache vs NGINX
• Node.js is a set of bindings for writing high-performance network
servers.
• Exposes only non-blocking I/O API's
• Stops us writing code that behaves badly.
Below is code for a simple http server in Node.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Now i got it
• Node.js is being used by many companies including big names like
Linkedin, WalMart in fact Linkedin's mobile backend is totally on
node.js.
• Excellent package manager npm(Node Package Manager).
• Installing packages: npm install <package>
• npm can also be used for dependency management just like maven.
• One more reason for Node.js's popularity is huge base of javascript
developers who can now write backend code too.
Production Usage ?
Thank You !!!
Time for a quick demo

Más contenido relacionado

La actualidad más candente

Nodejs - Building a RESTful API
Nodejs - Building a RESTful APINodejs - Building a RESTful API
Nodejs - Building a RESTful API
Sang Cù
 
Introduction to NodeJS
Introduction to NodeJSIntroduction to NodeJS
Introduction to NodeJS
Zahid Mahir
 

La actualidad más candente (20)

Introduction to node
Introduction to nodeIntroduction to node
Introduction to node
 
Vue.JS Hello World
Vue.JS Hello WorldVue.JS Hello World
Vue.JS Hello World
 
Breaking the eggshell: From .NET to Node.js
Breaking the eggshell: From .NET to Node.jsBreaking the eggshell: From .NET to Node.js
Breaking the eggshell: From .NET to Node.js
 
JavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
JavaScript as a Server side language (NodeJS): JSConf 2011, DhakaJavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
JavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
 
Build App with Nodejs - YWC Workshop
Build App with Nodejs - YWC WorkshopBuild App with Nodejs - YWC Workshop
Build App with Nodejs - YWC Workshop
 
NodeSummit - MEAN Stack
NodeSummit - MEAN StackNodeSummit - MEAN Stack
NodeSummit - MEAN Stack
 
MEAN stack
MEAN stackMEAN stack
MEAN stack
 
Beyond the MEAN Stack: Thinking Small with Node.js for the Enterprise
Beyond the MEAN Stack: Thinking Small with Node.js for the EnterpriseBeyond the MEAN Stack: Thinking Small with Node.js for the Enterprise
Beyond the MEAN Stack: Thinking Small with Node.js for the Enterprise
 
MongoDB MEAN Stack Webinar October 7, 2015
MongoDB MEAN Stack Webinar October 7, 2015MongoDB MEAN Stack Webinar October 7, 2015
MongoDB MEAN Stack Webinar October 7, 2015
 
MEAN Stack - Google Developers Live 10/03/2013
MEAN Stack - Google Developers Live 10/03/2013MEAN Stack - Google Developers Live 10/03/2013
MEAN Stack - Google Developers Live 10/03/2013
 
Intro to Knockout.JS for Salesforce1
Intro to Knockout.JS for Salesforce1Intro to Knockout.JS for Salesforce1
Intro to Knockout.JS for Salesforce1
 
Node.js tutoria for beginner
Node.js tutoria for beginnerNode.js tutoria for beginner
Node.js tutoria for beginner
 
Nodejs
NodejsNodejs
Nodejs
 
Nodejs - Building a RESTful API
Nodejs - Building a RESTful APINodejs - Building a RESTful API
Nodejs - Building a RESTful API
 
PHP Indonesia - Nodejs Web Development
PHP Indonesia - Nodejs Web DevelopmentPHP Indonesia - Nodejs Web Development
PHP Indonesia - Nodejs Web Development
 
Introduction to NodeJS
Introduction to NodeJSIntroduction to NodeJS
Introduction to NodeJS
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
NodeJS and what is actually does
NodeJS and what is actually doesNodeJS and what is actually does
NodeJS and what is actually does
 
Angular js introduction
Angular js introductionAngular js introduction
Angular js introduction
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 

Similar a Node.js primer

Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)
Ran Mizrahi
 

Similar a Node.js primer (20)

JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
 
Intro to node and non blocking io
Intro to node and non blocking ioIntro to node and non blocking io
Intro to node and non blocking io
 
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
 
Real time web
Real time webReal time web
Real time web
 
IBM and Node.js - Old Doge, New Tricks
IBM and Node.js - Old Doge, New TricksIBM and Node.js - Old Doge, New Tricks
IBM and Node.js - Old Doge, New Tricks
 
An introduction to Node.js
An introduction to Node.jsAn introduction to Node.js
An introduction to Node.js
 
What is Mean Stack Development ?
What is Mean Stack Development ?What is Mean Stack Development ?
What is Mean Stack Development ?
 
Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)Intro to node.js - Ran Mizrahi (27/8/2014)
Intro to node.js - Ran Mizrahi (27/8/2014)
 
Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)Intro to node.js - Ran Mizrahi (28/8/14)
Intro to node.js - Ran Mizrahi (28/8/14)
 
Javascript why what and how
Javascript why what and howJavascript why what and how
Javascript why what and how
 
Kalp Corporate Node JS Perfect Guide
Kalp Corporate Node JS Perfect GuideKalp Corporate Node JS Perfect Guide
Kalp Corporate Node JS Perfect Guide
 
Node js internal
Node js internalNode js internal
Node js internal
 
C# Async/Await Explained
C# Async/Await ExplainedC# Async/Await Explained
C# Async/Await Explained
 
"Node.js vs workers — A comparison of two JavaScript runtimes", James M Snell
"Node.js vs workers — A comparison of two JavaScript runtimes", James M Snell"Node.js vs workers — A comparison of two JavaScript runtimes", James M Snell
"Node.js vs workers — A comparison of two JavaScript runtimes", James M Snell
 
webservers
webserverswebservers
webservers
 
Meanstack overview
Meanstack overviewMeanstack overview
Meanstack overview
 
Stackato v3
Stackato v3Stackato v3
Stackato v3
 
Beginners Node.js
Beginners Node.jsBeginners Node.js
Beginners Node.js
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
02 Node introduction
02 Node introduction02 Node introduction
02 Node introduction
 

Último

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 

Último (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

Node.js primer

  • 1. Node.js That's what happens when JavaScript runs on Server too By Swapnil Mishra
  • 2. But Javascript on server ?
  • 3. • Powerful JavaScript engine which powers Google Chrome browser and Chrome OS. • Can also be considered as a Virtual Machine. • Node.js runs on the top of V8. • Utilizes the networking and parsing features which are done elegantly in V8. V8 made it possible
  • 4. • There are differences in the JavaScript which runs in browser vs node.js. e.g. There are no "window" or "document" objects. • Things which have been taken from V8 are asynchronous/non- blocking nature of JavaScript, Syntax, Data Structures(most of them). Which means everyones favorite closure/anonymous functions will work here too. • Things which have been added into it are Timers, Processes, Events, Buffers, Streams, Crypto, File Systems, HTTP, HTTPS and many more. Wait, so you are saying this node.js is same as the javascript which runs in browser ?
  • 5. • Model is evented rather than threaded. • Using an event loop, you ask it to do something and it will get back to you when its done.(we will see an example later) • Conventional languages are run in threaded environment where they create threads and just loop/pool in to wait for the result. Thats not the case with node.js. • Because of the evented nature it can perform Non-Blocking Network I/O. Oh cool tell me more about it !!
  • 6. Example of Blocking I/O public INeedData { public Data getMeSomeData(queryParam){ Data toBeReturned = db.query(queryParam); iAmBlockedTillQueryExecutes(); return toBeReturned; Blocking I/O } } // get data performs blocking I/O call because of which // iAmBlockedTillQueryExecutes() is blocked Why should i care about Non-Blocking I/O ?
  • 7. Think about what we really do on web server. • Grab Files • Call Databases • Pull from caches • Wait on other connections It's all I/O and we know that I/O latency of network is highest. Why should i care about Non-Blocking I/O ?
  • 8. • Totally and so does web-servers like Apache, IIS. • But threading is not free. • There are context switches and executions stacks take memory. What should we do then ? Hey but that's exactly why i use thread
  • 9. Use a single thread. Do little pieces of work and do them quickly. But single thread !!! We have been using multithreading from ages when UI used to be in "Java Swing" !!! Are you crazy ? Can it handle ? Event loop comes to the rescue
  • 12. The difference? • Apache uses one thread per connection. • NGINX does not use threads. It uses an event loop. So basically we have to change our programming model to something where we could easily write non-blocking code. public INeedData { public Data getMeSomeData(queryParam){ Data toBeReturned = db.query(queryParam,function(){ return toBeReturned;}); iAmNotBlockedTillQueryExecutes(); } } Apache vs NGINX
  • 13. • Node.js is a set of bindings for writing high-performance network servers. • Exposes only non-blocking I/O API's • Stops us writing code that behaves badly. Below is code for a simple http server in Node.js var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); Now i got it
  • 14. • Node.js is being used by many companies including big names like Linkedin, WalMart in fact Linkedin's mobile backend is totally on node.js. • Excellent package manager npm(Node Package Manager). • Installing packages: npm install <package> • npm can also be used for dependency management just like maven. • One more reason for Node.js's popularity is huge base of javascript developers who can now write backend code too. Production Usage ?
  • 15. Thank You !!! Time for a quick demo