SlideShare uma empresa Scribd logo
1 de 59
Baixar para ler offline
A practical introduction



Felix Geisendörfer                         Øredev 09.11.2011 (v1)
@felixge

Twitter / GitHub / IRC


    Felix Geisendörfer
     (Berlin, Germany)
Audience?
JavaScript?
Node.js?
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
#2
2nd most watched repository on GitHub
230 Contributors
0.6.0 was released 4 days ago (Nov 5)
Companies using node
• GitHub (for Downloads)
• Palm/HP (in WebOS)
• Yahoo! Mail
• Dow Jones & Company (for WJS social site)
• LinkedIn (Mobile Web App)
• 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');



$ node hello.js
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);


$ 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');


             $ 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');
});

            $ 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);

$ 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.0)


• 4700+ 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
• Actual 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

Mais conteúdo relacionado

Mais procurados

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
 
Shell Tips & Tricks
Shell Tips & TricksShell Tips & Tricks
Shell Tips & Tricks
MongoDB
 

Mais procurados (20)

Node.js in production
Node.js in productionNode.js in production
Node.js in production
 
Nodejs - A quick tour (v6)
Nodejs - A quick tour (v6)Nodejs - A quick tour (v6)
Nodejs - A quick tour (v6)
 
Node.js - A Quick Tour
Node.js - A Quick TourNode.js - A Quick Tour
Node.js - A Quick Tour
 
How to Test Asynchronous Code (v2)
How to Test Asynchronous Code (v2)How to Test Asynchronous Code (v2)
How to Test Asynchronous Code (v2)
 
Move Over, Rsync
Move Over, RsyncMove Over, Rsync
Move Over, Rsync
 
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
 
Building the Right Platform Architecture for Hadoop
Building the Right Platform Architecture for HadoopBuilding the Right Platform Architecture for Hadoop
Building the Right Platform Architecture for Hadoop
 
Running High Performance & Fault-tolerant Elasticsearch Clusters on Docker
Running High Performance & Fault-tolerant Elasticsearch Clusters on DockerRunning High Performance & Fault-tolerant Elasticsearch Clusters on Docker
Running High Performance & Fault-tolerant Elasticsearch Clusters on Docker
 
Server-Side JavaScript Developement - Node.JS Quick Tour
Server-Side JavaScript Developement - Node.JS Quick TourServer-Side JavaScript Developement - Node.JS Quick Tour
Server-Side JavaScript Developement - Node.JS Quick Tour
 
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
 
CoreOS + Kubernetes @ All Things Open 2015
CoreOS + Kubernetes @ All Things Open 2015CoreOS + Kubernetes @ All Things Open 2015
CoreOS + Kubernetes @ All Things Open 2015
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
Shell Tips & Tricks
Shell Tips & TricksShell Tips & Tricks
Shell Tips & Tricks
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with Examples
 
NodeJS
NodeJSNodeJS
NodeJS
 
Declare your infrastructure: InfraKit, LinuxKit and Moby
Declare your infrastructure: InfraKit, LinuxKit and MobyDeclare your infrastructure: InfraKit, LinuxKit and Moby
Declare your infrastructure: InfraKit, LinuxKit and Moby
 
Introduction to NodeJS with LOLCats
Introduction to NodeJS with LOLCatsIntroduction to NodeJS with LOLCats
Introduction to NodeJS with LOLCats
 
Elasticsearch for Logs & Metrics - a deep dive
Elasticsearch for Logs & Metrics - a deep diveElasticsearch for Logs & Metrics - a deep dive
Elasticsearch for Logs & Metrics - a deep dive
 
Openstack 簡介
Openstack 簡介Openstack 簡介
Openstack 簡介
 

Destaque

Destaque (8)

Node.js гэж юу вэ?
Node.js гэж юу вэ?Node.js гэж юу вэ?
Node.js гэж юу вэ?
 
Create simple api using node js
Create simple api using node jsCreate simple api using node js
Create simple api using node js
 
Getting Started with the Node.js LoopBack APi Framework
Getting Started with the Node.js LoopBack APi FrameworkGetting Started with the Node.js LoopBack APi Framework
Getting Started with the Node.js LoopBack APi Framework
 
Top Node.js Metrics to Watch
Top Node.js Metrics to WatchTop Node.js Metrics to Watch
Top Node.js Metrics to Watch
 
Building a Node.js API backend with LoopBack in 5 Minutes
Building a Node.js API backend with LoopBack in 5 MinutesBuilding a Node.js API backend with LoopBack in 5 Minutes
Building a Node.js API backend with LoopBack in 5 Minutes
 
Microservices with Node.js and RabbitMQ
Microservices with Node.js and RabbitMQMicroservices with Node.js and RabbitMQ
Microservices with Node.js and RabbitMQ
 
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
 

Semelhante a Nodejs a-practical-introduction-oredev

Semelhante a Nodejs a-practical-introduction-oredev (20)

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
 
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 - The New, New Hotness
Node.js - The New, New HotnessNode.js - The New, New Hotness
Node.js - The New, New Hotness
 
GeekCampSG - Nodejs , Websockets and Realtime Web
GeekCampSG - Nodejs , Websockets and Realtime WebGeekCampSG - Nodejs , Websockets and Realtime Web
GeekCampSG - Nodejs , Websockets and Realtime Web
 
FITC - Node.js 101
FITC - Node.js 101FITC - Node.js 101
FITC - Node.js 101
 
Introduction to node.js GDD
Introduction to node.js GDDIntroduction to node.js GDD
Introduction to node.js GDD
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
 
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
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
 
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
 
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?
 
Nodejs and WebSockets
Nodejs and WebSocketsNodejs and WebSockets
Nodejs and WebSockets
 
Developing realtime apps with Drupal and NodeJS
Developing realtime apps with Drupal and NodeJS Developing realtime apps with Drupal and NodeJS
Developing realtime apps with Drupal and NodeJS
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
 
Exploring Node.jS
Exploring Node.jSExploring Node.jS
Exploring Node.jS
 
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
 
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)
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
 
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
 
World of Node.JS
World of Node.JSWorld of Node.JS
World of Node.JS
 

Último

Último (20)

[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 

Nodejs a-practical-introduction-oredev