Introdution to Node.js
Upcoming SlideShare
Loading in...5
×
 

Introdution to Node.js

on

  • 1,576 views

 

Statistics

Views

Total Views
1,576
Views on SlideShare
1,549
Embed Views
27

Actions

Likes
0
Downloads
16
Comments
0

2 Embeds 27

http://coderwall.com 24
http://www.linkedin.com 3

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Welcome et. all.\n
  • This talk is about Node.js, or Node. Does anyone know what Node is, actually?\n
  • Node brings Evented I/O to the V8 JavaScript Engine. V8 is Google’s open source JavaScript engine, it’s used in Google Chrome.\n
  • Evented I/O makes Node lightning fast. While waiting for some I/O to happen, it continues to utilize the CPU. Let’s skip to an example, shall we?\n
  • Reading a file from disk, slow I/O. See this ruby example. If f.read takes X seconds, execution is blocked for X seconds - nothing else happens.\n
  • Node goes do other stuff while waiting for I/O to complete. When the data is ready (or an error occurred), execute the callback function to continue processing the data. This is Coffee script btw.\n
  • Okay, plain JavaScript for the non-coffeescripters.\n
  • \n
  • Modules allow you to organise functionality in logic units. These units can’t interfere with each other, because they are .. modular.\n\nTell about how JS files loaded all use the same variable scope.\n
  • This is how you load a module in Coffee Script. Then you can call a method from that module.\n
  • The hello module implementation.\n
  • Events are an awesome pattern. Here’s an example.\n
  • This is how you’d write the implementation. Of course in coffee script. Now wait for someone to call hello_world.generate()\n
  • NPM\nNode was designed from the ground up. There is no ‘sync-node’, everything was designed to be async. Including packages for everything.\n
  • A simple Node HTTP Server. When a request comes in we set a 2 second timer and then respond. Each request should take about 2 seconds to complete. \n
  • Take a 1000 **concurrent** users; who’ll send 10 requests in succession each. How long does it take to run this test? For time’s sake, I’ll show you a video instead of running it live.\n\n10k requests at 2 seconds each on 1 server: 20k seconds, about 5.5 hours\n
  • Show em! - http://vimeo.com/ariejan/jmeter-benchmark-of-nodejs\n
  • Be amazed. Requests return on average after 2007ms. This means Node is not waiting 2 seconds before accepting the next request. When a timer goes, the response is send immediately.\n
  • How about testing?\n
  • Async tests! So they aren’t blocking on I/O either and therefore are lightning fast, just like Node is!\n
  • JSONToolbox. A Node project by yours truly.\n
  • Are my vows honoured? Yes the are ;-)\n
  • DO YOU SEE IT?! Yes, 10 specs, including starting and shutting down a server, in 0.087s!\n
  • Okay, first, here’s what we got. But, this is an API. Where's my capybara?\n
  • We’ll need full-stack testing here to make sure everything works ‘in the browser’. We’ll use Zombie.js\n
  • We’ll describe the API Service. Batches are executed sequentially. Tests within a batch are run asychroniously.\n
  • Some packages I’ve found to be useful.\n
  • Some links\n
  • Any questions?\n
  • Bye bye\n

Introdution to Node.js Introdution to Node.js Presentation Transcript

  • What’s Node?
  • What’s Node?Evented I/O for V8 JavaScript
  • Evented I/O• Don’t wait for I/O to complete• Instead, do useful stuff on the CPU• When I/O has finished, execute callback code
  • An ExampleFile.open(/etc/motd, w) do |f| logger.debug(f.read)end
  • An Examplefs.readFile /etc/motd, (err, data) -> console.log(data) unless err
  • An Examplefs.readFile(/etc/motd, function(err, data) { if (!err) { return console.log(data); }});
  • Evented I/O• Prior to Node, no I/O libraries for JavaScript
  • Modules• Organise functionality• No interference with other code
  • Loading a module hello = require(hello) hello.world()
  • hello.coffeeexports.world = () -> console.log(Hello World)hello = require(hello)hello.world()
  • Listening to Eventshello_world = new HelloWorld()hello_world.on generated, (text) -> console.log text
  • Emitting Eventsclass HelloWorld extends EventEmitter generate: -> this.emit generated, Hello World!hello_world.on generated, (text) -> console.log text
  • Gems?• The Node Package Manage (NPM) is awesome• Find packages at http://search.npmjs.org/• Node, and packages, are asynchronous by design
  • Consider thishttp = require(http);http.createServer (req, res) -> respond_later = () -> res.writeHead 200, {Content-Type: text/plain} res.end Thank you for waiting 2 secondsn setTimeout respond_later, 2000.listen(1337, "127.0.0.1")
  • Got Speed?1000 concurrent users 10 requests / user 1 node serverWhat’s your estimate?
  • Watch it online... http://vimeo.com/ariejan/jmeter- benchmark-of-nodejs
  • Got Speed? 1000 concurrent users 10 requests / user 1 node serverAverage Response Time: 2007 ms
  • It’s got speed... ... but does it offer quality?
  • Take your vows• Asynchronous Behaviour Driven Development
  • An example, again... JSONToolbox.com
  • Honour your vows API Service Random password with a negative length of -32 ✓ should receive a password of length 32 Random password with a length > 256 ✓ should receive a password of length 256 Random password with length 40 ✓ should receive a password of length 40 Random password with default length ✓ should receive a valid response ✓ should receive a password of length 12 by default User agent ✓ should report the user agent string IP Address ✓ should report client IP address as 127.0.0.1 Ping ✓ should respond with pong✓ OK » 10 honored (0.087s)
  • Honour your vows✓ OK » 10 honored (0.087s)
  • Got Ping?app.get /api/ping, (req, res) -> res.contentType(application/json) res.send { "pong" : true }
  • Enter Zombies• Headless, full-stack testing• It’s capybara, but faster• Oh, of course it’s asynchronous as well
  • Got Pong?vows .describe(API Service) .addBatch Ping: topic: -> zombie.visit("http://127.0.0.1/api/ping", this.callback) should respond with pong: (browser) -> JSON.parse(browser.text(body))[pong].should.be.true
  • Useful stuff• coffee-script - duh!• express - Sinatra inspired web framework• socket.io - cross-browser websockets• vows, zombie - BDD• nodeunit - TDD / Unit testing• redis - Fast in memory DB, with PubSub support
  • Link Fest• http://nodejs.org• http://vowsjs.org• http://zombie.labnotes.org• http://search.npmjs.org• https://github.com/ariejan/jsontoolbox• https://github.com/ariejan/apocalypse
  • Q&A
  • Thank you! Good Night!