SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

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
 

Tendances (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
 

Similaire à 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
 

Similaire à 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
 

Dernier

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Dernier (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

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