Node js presentation


Published on

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

No notes for slide

Node js presentation

  1. 1. Server side Javascript environment
  2. 2. What is node.js? Why another server side technology? Node.js is a server-side JavaScript environment that uses an asynchronous event-driven model. This allows Node.js to get excellent performance based on the architectures of many Internet applications. Most regular Web Servers like Apache uses OS threads for request handling. That means that every request the server handles will spawn a OS thread and the web server won't release the thread until the request finish. Most of the time OS threads wait till some I/O operation finish: var result = query("select * from users"); for(user in result){      //You get the idea } In this case the OS thread just sits and waits the I/O operation returns to resume the flow. Every OS thread takes some memory, so regular servers can't handle many simultaneous connections without penalizing the system performance.
  3. 3. node.js carachteristics <ul><ul><li>Implemented on top of the V8 js engine (Chromium and Google Chrome engine). </li></ul></ul><ul><ul><li>Programs are written in plain old javascript, we use the same language for client and server (Die Ruby Die!) </li></ul></ul><ul><ul><li>Does I/O better: query(&quot;select * from users&quot;, function(data, status){   //Do something really cool with users }); </li></ul></ul><ul><ul><li>Files I/O and db queries are non-blocking </li></ul></ul><ul><ul><li>Great performance even handling a big number of users </li></ul></ul><ul><ul><li>It gets better every time google improves V8 </li></ul></ul><ul><ul><li>It has his own package manager called npm (node package manager) </li></ul></ul>
  4. 4. Alternatives <ul><ul><li>Ruby Event Machine </li></ul></ul><ul><ul><li>Python Twisted </li></ul></ul>
  5. 5. hands on code: hello world! <?php       //helloworld.php      echo &quot;hello&quot;;      sleep(2);      echo &quot;world!&quot;; ?> //helloworld.js setTimeout(function(){      console.log(&quot;world!&quot;); }, 2000); console.log(&quot;hello&quot;); There is no waisted time on the node program, the thread is idle not waiting
  6. 6. hands on code: http server var http = require(&quot;http&quot;) ; var server = http.createServer(function(req, res){      res.writeHead(200, {&quot;content-type&quot;:&quot;text/plain&quot;});      res.end(&quot;Hello World!n&quot;); }).listen(8000); Headers of the request are: HTTP/1.1 200 OK content-type: text/plain Connection: keep-alive Transfer-Encoding: chunked
  7. 7. hands on code: not only http var net = require(&quot;net&quot;) ; net.createServer(function(socket){      socket.on(&quot;data&quot;, function(data){        socket.write(data);      }); }).listen(8000);
  8. 8. hands on code: telnet chat var net = require(&quot;net&quot;); var clients = []; net.createServer(function(socket){    clients.push(socket);         socket.on(&quot;data&quot;, function(data){      for(var i = 0; i < clients.length; i++){              if(clients[i] == socket) continue;        clients[i].write(data);                  }        });         socket.on(&quot;end&quot;, function(){      var index = clients.indexOf(socket);      clients.splice(index, 1);    });   }).listen(8000);
  9. 9. hands on code: Async I/O var fs = require(&quot;fs&quot;)   , sys = require(&quot;sys&quot;);; fs.readFile(&quot;/home/cherta/workspace/samples/foo.txt&quot;, function(err, data){     if(err) throw err;     sys.puts(data) }); console.log(&quot;Antes de foo?&quot;);
  10. 10. What is node.js? It's a set of tools and libraries living on top of the V8. What is not node.js? A super framework that would change your life. Assuming that your life will change for a crappy piece of software
  11. 11. Working on top of node There are killer frameworks that will make your life easier using node. <ul><ul><li>Express web framework </li></ul></ul><ul><ul><li>Socket IO </li></ul></ul>
  12. 12. Express <ul><ul><li>Web framework working on top of node.js </li></ul></ul><ul><ul><li>High Performance </li></ul></ul><ul><ul><li>Template engines support (jade, ejs) </li></ul></ul><ul><ul><li>CSS engines support (sass, less, stylus) </li></ul></ul><ul><ul><li>Partials support  </li></ul></ul>var app = express.createServer(); app.get('/', function(req, res){     res.send('Hello World'); }); app.listen(3000);
  13. 13. Socket I/O Socket.IO is a Node.JS project that makes WebSockets and realtime possible in all browsers. It also enhances WebSockets by providing built-in multiplexing, horizontal scalability, automatic JSON encoding/decoding, and more.
  14. 14. gracias