Introdution to Node.js


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • 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 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! -\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

    1. 1. What’s Node?
    2. 2. What’s Node?Evented I/O for V8 JavaScript
    3. 3. 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
    4. 4. An, w) do |f| logger.debug(
    5. 5. An Examplefs.readFile /etc/motd, (err, data) -> console.log(data) unless err
    6. 6. An Examplefs.readFile(/etc/motd, function(err, data) { if (!err) { return console.log(data); }});
    7. 7. Evented I/O• Prior to Node, no I/O libraries for JavaScript
    8. 8. Modules• Organise functionality• No interference with other code
    9. 9. Loading a module hello = require(hello)
    10. 10. = () -> console.log(Hello World)hello = require(hello)
    11. 11. Listening to Eventshello_world = new HelloWorld()hello_world.on generated, (text) -> console.log text
    12. 12. Emitting Eventsclass HelloWorld extends EventEmitter generate: -> this.emit generated, Hello World!hello_world.on generated, (text) -> console.log text
    13. 13. Gems?• The Node Package Manage (NPM) is awesome• Find packages at• Node, and packages, are asynchronous by design
    14. 14. 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, "")
    15. 15. Got Speed?1000 concurrent users 10 requests / user 1 node serverWhat’s your estimate?
    16. 16. Watch it online... benchmark-of-nodejs
    17. 17. Got Speed? 1000 concurrent users 10 requests / user 1 node serverAverage Response Time: 2007 ms
    18. 18. It’s got speed... ... but does it offer quality?
    19. 19. Take your vows• Asynchronous Behaviour Driven Development
    20. 20. An example, again...
    21. 21. 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 Ping ✓ should respond with pong✓ OK » 10 honored (0.087s)
    22. 22. Honour your vows✓ OK » 10 honored (0.087s)
    23. 23. Got Ping?app.get /api/ping, (req, res) -> res.contentType(application/json) res.send { "pong" : true }
    24. 24. Enter Zombies• Headless, full-stack testing• It’s capybara, but faster• Oh, of course it’s asynchronous as well
    25. 25. Got Pong?vows .describe(API Service) .addBatch Ping: topic: -> zombie.visit("", this.callback) should respond with pong: (browser) -> JSON.parse(browser.text(body))[pong]
    26. 26. Useful stuff• coffee-script - duh!• express - Sinatra inspired web framework• - cross-browser websockets• vows, zombie - BDD• nodeunit - TDD / Unit testing• redis - Fast in memory DB, with PubSub support
    27. 27. Link Fest••••••
    28. 28. Q&A
    29. 29. Thank you! Good Night!