SlideShare ist ein Scribd-Unternehmen logo
1 von 77
A practical introduction



Felix Geisendörfer                Munich Node.js User Group 01.12.2011 (v2)
@felixge

Twitter / GitHub / IRC


    Felix Geisendörfer
     (Berlin, Germany)
Node.js in Munich?
Used node before?
Node in production?
History
Feb 16, 2009




Ryan Dahl starts the node project (first commit)
~June, 2009




Discovered node.js (v0.0.6)
transloadit.com
Core Contributor

                    &

              Module Author



node-mysql                      node-formidable



                                  + 30 other modules
Sep 29, 2009




Isaac Schlueter starts the npm package manager
                  (first commit)
Nov 7, 2009




Ryan’s talk at JSConf.EU gets people excited about node
Today
#1
Most watched repository on GitHub
...
0.6.3 was released 6 days ago (Nov 25)
Companies using node
• LinkedIn (Mobile Web App)
• Ebay (Data retrieval gateway)
• GitHub (for Downloads)
• Palm/HP (in WebOS)
• Yahoo! Mail
• Dow Jones & Company (for WJS social site)
• Rackspace (Cloudkick monitoring)
• Voxxer (Push to Talk mobile app)
Installing
$ git clone 
git://github.com/joyent/node.git
$ cd node
$ git checkout v0.6.0
$ ./configure
$ sudo make install
     (windows users: download node.exe)
Hello World

hello.js
console.log('Hello World');
Hello World

hello.js
console.log('Hello World');



$
Hello World

hello.js
console.log('Hello World');



$ node
Hello World

hello.js
console.log('Hello World');



$ node hello.js
Hello World

hello.js
console.log('Hello World');



$ node hello.js
Hello World
Hello World (REPL)
Hello World (REPL)


 $
Hello World (REPL)


 $ node
Hello World (REPL)


 $ node
 >
Hello World (REPL)


 $ node
 > console.log('Hello World')
Hello World (REPL)


 $ node
 > console.log('Hello World')
 Hello World
Http Server
http_server.js
var http = require('http');
var server = http.createServer(function(req, res) {
  res.end('Hi, how are you?');
});

server.listen(8080);
Http Server
http_server.js
var http = require('http');
var server = http.createServer(function(req, res) {
  res.end('Hi, how are you?');
});

server.listen(8080);


$ node http_server.js
Http Server
http_server.js
var http = require('http');
var server = http.createServer(function(req, res) {
  res.end('Hi, how are you?');
});

server.listen(8080);


$ node http_server.js    $ curl localhost:8080
                         Hi, how are you?
“Come on, server side JS
 has been around since
        1996”
What is so special
 about node?
Speed
Speed

• Node can do ~6000 http requests / sec per
  CPU core (hello world, 1kb response,)


• It is no problem to handle thousands of
  concurrent connections which are
  moderately active
V8 JavaScript Engine
V8 JavaScript Engine

• Developed by Google in Aarhus, Denmark
  for Google Chrome


• Translates JavaScript in Assembly Code

• Crankshaft JIT (enabled in 0.6.0 by default)
V8 JavaScript Engine

• You can expect to be within ~10x of C
  performance usually


• Certain code can run within 2-3x of C

• Getting faster all the time
Non-Blocking I/O
Blocking I/O
read_file_sync.js

var fs = require('fs');
var one = fs.readFileSync('one.txt', 'utf-8');
console.log('Read file one');
var two = fs.readFileSync('two.txt', 'utf-8');
console.log('Read file two');
Blocking I/O
read_file_sync.js

var fs = require('fs');
var one = fs.readFileSync('one.txt', 'utf-8');
console.log('Read file one');
var two = fs.readFileSync('two.txt', 'utf-8');
console.log('Read file two');


             $ node read_file_sync.js
Blocking I/O
read_file_sync.js

var fs = require('fs');
var one = fs.readFileSync('one.txt', 'utf-8');
console.log('Read file one');
var two = fs.readFileSync('two.txt', 'utf-8');
console.log('Read file two');


             $ node read_file_sync.js
             Read file one
Blocking I/O
read_file_sync.js

var fs = require('fs');
var one = fs.readFileSync('one.txt', 'utf-8');
console.log('Read file one');
var two = fs.readFileSync('two.txt', 'utf-8');
console.log('Read file two');


             $ node read_file_sync.js
             Read file one
             Read file two
Non-Blocking I/O
read_file_async.js
var fs = require('fs');
fs.readFile('one.txt', 'utf-8', function(err, data) {
  console.log('Read file one');
});
fs.readFile('two.txt', 'utf-8', function(err, data) {
  console.log('Read file two');
});
Non-Blocking I/O
read_file_async.js
var fs = require('fs');
fs.readFile('one.txt', 'utf-8', function(err, data) {
  console.log('Read file one');
});
fs.readFile('two.txt', 'utf-8', function(err, data) {
  console.log('Read file two');
});

            $ node read_file_async.js
Non-Blocking I/O
read_file_async.js
var fs = require('fs');
fs.readFile('one.txt', 'utf-8', function(err, data) {
  console.log('Read file one');
});
fs.readFile('two.txt', 'utf-8', function(err, data) {
  console.log('Read file two');
});

            $ node read_file_async.js
            Read file two
Non-Blocking I/O
read_file_async.js
var fs = require('fs');
fs.readFile('one.txt', 'utf-8', function(err, data) {
  console.log('Read file one');
});
fs.readFile('two.txt', 'utf-8', function(err, data) {
  console.log('Read file two');
});

            $ node read_file_async.js
            Read file two
            Read file one
Blocking I/O

Read one.txt (20ms)

                                Read two.txt (10ms)

              Total duration (30ms)
Non-Blocking I/O

Read one.txt (20ms)

Read two.txt (10ms)

      Total duration (20ms)
Non-Blocking I/O
• Close to ideal for high concurrency / high
  throughput, single execution stack


• Forces you to write more efficient code by
  parallelizing your I/O


• Feels pretty much like AJAX in the browser
WebSockets
  (Push)
WebSockets

•   Persistent connection between browser/server


•   Very hard / awkward to do on traditional stacks


•   Hard to scale on traditional stacks
Socket.IO (community module)
       •   WebSocket

       •   Adobe® Flash® Socket

       •   AJAX long polling

       •   AJAX multipart streaming

       •   Forever Iframe

       •   JSONP Polling

Chooses most capable transport at runtime!
Streams
“Streams are to time as arrays are to space.”
                    -- Jed Schmidt @ JSConf.eu 2010
Streams in node.js

•   Readable


•   Writable


•   Both
Streams
var http = require('http');
var server = http.createServer(function(req, res) {
  req.on('data', console.log);
});
server.listen(8080);
Streams
var http = require('http');
var server = http.createServer(function(req, res) {
  req.on('data', console.log);
});
server.listen(8080);

$ node stream.js &
$ curl -F file=@stream.js localhost:8080
------------------------------41e92562223e
Content-Disposition: form-data; name="file"; filename
Content-Type: application/octet-stream

var http = require('http');
var server = http.createServer(function(req, res) {
Streams
var http = require('http');
var spawn = require('child_process').spawn;

http.createServer(function(req, res) {
  var params = req.url.split('/');
  var args = [params[1], '-resize', params[2], '-'];
  var convert = spawn('convert', args);

  convert.stdout.pipe(res);
}).listen(8080);
On Github


felixge/node-convert-example
NPM package manager
NPM
• Puts dependencies in the right place, then
  gets out of your way


• No modification of a global load path
  (require.paths is gone in 0.6.x)


• 5369+ Modules in npm, > 10 new
  modules / day
So what is node good
        for?
Use cases

• WebSockets/Push applications

• Proxying data streams

• Backend for single page apps
Use cases

• Spawning other programs (processes) to do
  work / IPC


• Parallelizing I/O
So what is node not
     good for?
Anti-Use cases
• Hard Realtime Systems

• Number crunching / huge in-memory
  datasets


• (CRUD apps)
Join the Community

• Mailing list (nodejs, nodejs-dev)

• IRC (#node.js) - 700+ User online
Thank you!
Questions?




   @felixge
Thank you!
Bonus Slide
What’s next?

• Domains

• Improved Stream API

Weitere ähnliche Inhalte

Was ist angesagt?

Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
Tom Croucher
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scale
Tom Croucher
 
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming  Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Tom Croucher
 
Building servers with Node.js
Building servers with Node.jsBuilding servers with Node.js
Building servers with Node.js
ConFoo
 

Was ist angesagt? (20)

Node.js in production
Node.js in productionNode.js in production
Node.js in production
 
Dirty - How simple is your database?
Dirty - How simple is your database?Dirty - How simple is your database?
Dirty - How simple is your database?
 
Node.js - A Quick Tour
Node.js - A Quick TourNode.js - A Quick Tour
Node.js - A Quick Tour
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
 
Nginx-lua
Nginx-luaNginx-lua
Nginx-lua
 
A language for the Internet: Why JavaScript and Node.js is right for Internet...
A language for the Internet: Why JavaScript and Node.js is right for Internet...A language for the Internet: Why JavaScript and Node.js is right for Internet...
A language for the Internet: Why JavaScript and Node.js is right for Internet...
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with Examples
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
Introduction to Nodejs
Introduction to NodejsIntroduction to Nodejs
Introduction to Nodejs
 
Node.js - Best practices
Node.js  - Best practicesNode.js  - Best practices
Node.js - Best practices
 
How to Test Asynchronous Code (v2)
How to Test Asynchronous Code (v2)How to Test Asynchronous Code (v2)
How to Test Asynchronous Code (v2)
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
 
Move Over, Rsync
Move Over, RsyncMove Over, Rsync
Move Over, Rsync
 
NodeJS
NodeJSNodeJS
NodeJS
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scale
 
Redis - Usability and Use Cases
Redis - Usability and Use CasesRedis - Usability and Use Cases
Redis - Usability and Use Cases
 
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming  Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
 
Building servers with Node.js
Building servers with Node.jsBuilding servers with Node.js
Building servers with Node.js
 
Lua tech talk
Lua tech talkLua tech talk
Lua tech talk
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.js
 

Andere mochten auch

Andere mochten auch (10)

Anatomy of a Modern Node.js Application Architecture
Anatomy of a Modern Node.js Application Architecture Anatomy of a Modern Node.js Application Architecture
Anatomy of a Modern Node.js Application Architecture
 
Building an alarm clock with node.js
Building an alarm clock with node.jsBuilding an alarm clock with node.js
Building an alarm clock with node.js
 
It jobs road show
It jobs road showIt jobs road show
It jobs road show
 
Change RelationalDB to GraphDB with OrientDB
Change RelationalDB to GraphDB with OrientDBChange RelationalDB to GraphDB with OrientDB
Change RelationalDB to GraphDB with OrientDB
 
Firebase slide
Firebase slideFirebase slide
Firebase slide
 
Forensic Tools for In-Depth Performance Investigations
Forensic Tools for In-Depth Performance InvestigationsForensic Tools for In-Depth Performance Investigations
Forensic Tools for In-Depth Performance Investigations
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Node Foundation Membership Overview 20160907
Node Foundation Membership Overview 20160907Node Foundation Membership Overview 20160907
Node Foundation Membership Overview 20160907
 
The Enterprise Case for Node.js
The Enterprise Case for Node.jsThe Enterprise Case for Node.js
The Enterprise Case for Node.js
 

Ähnlich wie Node.js - A practical introduction (v2)

nodejs_at_a_glance.ppt
nodejs_at_a_glance.pptnodejs_at_a_glance.ppt
nodejs_at_a_glance.ppt
WalaSidhom1
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
Richard Lee
 

Ähnlich wie Node.js - A practical introduction (v2) (20)

Introduction to NodeJS with LOLCats
Introduction to NodeJS with LOLCatsIntroduction to NodeJS with LOLCats
Introduction to NodeJS with LOLCats
 
Node.js: The What, The How and The When
Node.js: The What, The How and The WhenNode.js: The What, The How and The When
Node.js: The What, The How and The When
 
Building and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsBuilding and Scaling Node.js Applications
Building and Scaling Node.js Applications
 
Nodejs and WebSockets
Nodejs and WebSocketsNodejs and WebSockets
Nodejs and WebSockets
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
 
nodejs_at_a_glance.ppt
nodejs_at_a_glance.pptnodejs_at_a_glance.ppt
nodejs_at_a_glance.ppt
 
Practical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsPractical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.js
 
Node.js - The New, New Hotness
Node.js - The New, New HotnessNode.js - The New, New Hotness
Node.js - The New, New Hotness
 
Introducing Node.js in an Oracle technology environment (including hands-on)
Introducing Node.js in an Oracle technology environment (including hands-on)Introducing Node.js in an Oracle technology environment (including hands-on)
Introducing Node.js in an Oracle technology environment (including hands-on)
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 Engine
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talk
 
Node.js 101 with Rami Sayar
Node.js 101 with Rami SayarNode.js 101 with Rami Sayar
Node.js 101 with Rami Sayar
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
 
Node.js - async for the rest of us.
Node.js - async for the rest of us.Node.js - async for the rest of us.
Node.js - async for the rest of us.
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
node.js - Fast event based web application development
node.js - Fast event based web application developmentnode.js - Fast event based web application development
node.js - Fast event based web application development
 
Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?
 
New kid on the block node.js
New kid on the block node.jsNew kid on the block node.js
New kid on the block node.js
 
Meetup RomaJS - introduzione interattiva a Node.js - Luca Lanziani - Codemoti...
Meetup RomaJS - introduzione interattiva a Node.js - Luca Lanziani - Codemoti...Meetup RomaJS - introduzione interattiva a Node.js - Luca Lanziani - Codemoti...
Meetup RomaJS - introduzione interattiva a Node.js - Luca Lanziani - Codemoti...
 
Hidden pearls for High-Performance-Persistence
Hidden pearls for High-Performance-PersistenceHidden pearls for High-Performance-Persistence
Hidden pearls for High-Performance-Persistence
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 

Node.js - A practical introduction (v2)

Hinweis der Redaktion

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. #24 on the mailing list\n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. Mention difference between node / other platforms where you’re implicitly embedded inside a webserver\n
  31. Mention difference between node / other platforms where you’re implicitly embedded inside a webserver\n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. If that is not fast enough for what you’re doing, use C\n
  39. \n
  40. \n
  41. \n
  42. \n
  43. Now you may say:\n- This is terrible\n- I have to write 7 lines instead of 5\n- And it’s in the wrong order\n\n
  44. Now you may say:\n- This is terrible\n- I have to write 7 lines instead of 5\n- And it’s in the wrong order\n\n
  45. Now you may say:\n- This is terrible\n- I have to write 7 lines instead of 5\n- And it’s in the wrong order\n\n
  46. \n
  47. \n
  48. \n
  49. A keen observer may note that the same is achievable with threads\n* But how many of you think threading is easier than making an AJAX call\n* Threads require significant more resources than the non-blocking I/O node is doing\n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. 10 lines of code\n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n