Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Tom Hughes-Croucher - Up and Running with Node.js

Web Directions
November 06, 2011

Tom Hughes-Croucher - Up and Running with Node.js

Learn how to build high performance Internet and web applications with Node.js. In is session Tom Hughes-Croucher will demonstrate how to quickly build a high performance chat server using Node.js. This live coding exercise will provide a real insight into what it looks like to build a project in server-side Javascript. We will also cover how to deploy Node applications in production and look at just how far Node can really scale… A million connections and beyond?

Web Directions

November 06, 2011
Tweet

More Decks by Web Directions

Other Decks in Technology

Transcript

  1. A language for the Internet Why JavaScript and Node.js is

    right for Internet Applications Tom Hughes-Croucher @sh1mmer
  2. Node.js? • Server Side JavaScript runtime • Built on top

    of V8 JavaScript engine from Google Chrome • Non-blocking I/O APIs • Easy to extend APIs and modules
  3. $Enki:~ $ node > 3 > 2 > 1 false

    > true == 1 true > true === 1 false
  4. > console.log('Hello World'); Hello World > .help .clear Break, and

    also clear the local context. .exit Exit the prompt .help Show repl options > .clear Clearing context... > .exit Enki:~ $
  5. Enki:~ $ node > var foo = "bar"; > foo;

    'bar' > .clear Clearing context... > foo ReferenceError: foo is not defined at [object Context]:1:1 at Interface.<anonymous> (repl:98:19) at Interface.emit (events:27:15) at Interface._ttyWrite (readline:295:12) at Interface.write (readline:132:30) at Stream.<anonymous> (repl:79:9) at Stream.emit (events:27:15) at IOWatcher.callback (net:489:16)
  6. var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type':

    'text/plain'}); res.end('Hello World\n'); }).listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/');
  7. http.createServer(function (req, res) { }).listen(8124, "127.0.0.1"); //create an http server

    //when ‘stuff’ happens call this anonymous function //listen on port 8124 of the IP 127.0.0.1
  8. http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); })

    //when ‘stuff’ happens my function fires //I get a request object and a response object //I write to the response object header //HTTP status 200 and content-type ‘text/plain’ //close the response with the body: //Hello World
  9. var http = require('http'); server = http.createServer(); server.on('request', function (req,

    res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/');
  10. var http = require('http'); server = http.createServer(); server.on('request', function (req,

    res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 1. Evaluate 'Main'
  11. var http = require('http'); server = http.createServer(); server.on('request', function (req,

    res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 1. variables: http -> http module server -> http server listeners: server.request -> function
  12. var http = require('http'); server = http.createServer(); server.on('request', function (req,

    res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 2. Event Loop *
  13. var http = require('http'); server = http.createServer(); server.on('request', function (req,

    res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 2. Do we have active listeners? listeners: server.request -> function Yes! Wait for listeners. *
  14. var http = require('http'); server = http.createServer(); server.on('request', function (req,

    res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 3. Event Calls
  15. var http = require('http'); server = http.createServer(); server.on('request', function (req,

    res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 3. 'request' is called. Since listeners: server.request -> function Call function
  16. var http = require('http'); server = http.createServer(); server.on('request', function (req,

    res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 3. Loop! (go to Step 2.)
  17. //node.js style JavaScript query = mysql.query('SELECT * FROM data'); query.on(‘result’,

    function(result) { for(var i=0;i<length;i++) { var row = result.rows[i]; //do something with the row } });
  18. //threaded style JavaScript result = mysql.query('SELECT * FROM data'); for(var

    i=0;i<length;i++) { var row = result.rows[i]; //do something with the row }
  19. var x = "I am a string" ~1ns Running 1

    instruction 2ns Data from l1 cpu cache 5ns Data from l2 cpu cache 80ns Data from ram
  20. result = mysql.query('SELECT * FROM data'); ~100ms Time to run

    a query in database 50ms Time to roundtrip query over the network