A slightly advanced introduction to node.js


Published on

Slides from my talk about node.js which I gave at jsFoo. More info at http://sudarmuthu.com/blog/introduction-to-node-js-at-jsfoo

Published in: Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

A slightly advanced introduction to node.js

  1. An Introduction to Node.JS<br />Sudar Muthu (@sudarmuthu)<br />Research Engineer<br />Yahoo Labs<br />http://sudarmuthu.com<br />
  2. Agenda<br />What Node.JS is<br />What Node.JS is not<br />Why Node.JS?<br />Using Node.JS<br />As a interactive shell<br />As a Server<br />As a Client<br />Common Modules<br />Terminologies<br />Questions<br />
  3. What is Node.JS<br />Provides Evented, non-blocking I/O<br />Built on Google’s V8<br />JavaScript Programming Environment<br />Supports C/C++ based addons<br />Supports CommonJS Module format<br />Is fast .. infact very fast.<br />
  4. What is Node.JS<br />Similar to <br />EventMachine in Ruby<br />Twisted in Python<br /> But provides Evented IO as part of the language construct itself and not as a library.<br />
  5. Node.JS is not …<br />Ruby on Rails<br />Django<br />Codeigniter<br />Node.JS is bare bone and the community are making stuff like Express, connect etc.<br />
  6. Why Node.JS?<br />Code like this<br />var result = db.query("select..");<br />// use result<br />either blocks the entire process or<br />implies multiple execution stacks (threads).<br />
  7. Why Node.JS?<br />But a line of code like this<br />db.query("select..", function (result) {<br /> // use result<br />});<br />allows the program to return to the<br />event loop immediately. No more<br />unnecessary threads.<br />
  8. Demo of Callback<br />// execute the callback after 2 seconds<br />setTimeout(function () {<br />console.log("World!");<br />}, 2000);<br />// print in console<br />console.log("Hello");<br />https://github.com/sudar/jsfoo/blob/master/callback.js<br />
  9. Using Node.JS<br />Install Node.JS<br />Install NPM<br />Install other modules you want by doing<br />npm install <module_name><br />You are good to go<br />
  10. Node.JS – as an interactive shell<br />Similar to Python’s shell<br />$> node<br />> 3 + 1<br />4<br />> true != false<br />true<br />>.help<br />>.exit<br />
  11. Node.JS – As a server<br />var http = require('http'); // require the http module<br />// create a server<br />http.createServer(function (req, res) {<br /> // call this function when a request is received<br /> res.writeHead(200, {<br /> 'Content-Type': 'text/plain'<br /> });<br /> // send this as part of the response<br />res.end('Hello Worldn');<br />}).listen(1337, ""); // listen on port 1337<br />// debug information<br />console.log('Server running at');<br />https://github.com/sudar/jsfoo/blob/master/http-server.js<br />
  12. Node.JS – As a client<br />var http = require('http'); // require the needed modules<br />// make the request object<br />var request = http.request({<br /> 'host': 'sudarmuthu.com',<br /> 'port': 80,<br /> 'path': '/',<br /> 'method': 'GET'<br />});<br />// assign callbacks<br />request.on('response', function (response) {<br />console.log('Response Status Code: ' + response.statusCode);<br />response.on('data', function (data) {<br />console.log('Body: ' + data);<br /> });<br />});<br />https://github.com/sudar/jsfoo/blob/master/http-client.js<br />
  13. Core Modules<br />Processes<br />Filesystem<br />Networking<br />Utilities<br />The entire list can be found at http://nodejs.org/docs/v0.4.12/api/<br />
  14. Core Modules - Processes<br />Node allows you to analyze your process and manage external process<br />Available Modules<br />process<br />child_process<br />Code samples: http://github.com/sudar/jsfoo/process.js<br />
  15. Core Modules - Filesystem<br />Low level API to manipulate files<br />Available Modules<br />fs<br />path<br />Code Samples: http://github.com/sudar/jsfoo/filesystem.js<br />
  16. Core Modules - Networking<br />Available Modules<br />net<br />dgram<br />http<br />tls<br />https<br />dns<br />Code Samples: https://github.com/sudar/jsfoo/blob/master/dns.js<br />
  17. Core Modules - Utilities<br />Provides utility methods<br />Available Modules<br />console<br />util<br />Code Samples: https://github.com/sudar/jsfoo/blob/master/console.js<br />
  18. Node.JS is useful for..<br />Writing highly concurrent server applications<br />Sharing application logic between server and client<br />Peer-to-peer web applications using websockets<br />
  19. Terminologies<br />NPM – Package manager (like apt-get)<br />Modules – Plugins or add-ons for Node.JS<br />Express – MVC framework (like RoR)<br />Jade – Template Engine (like Smarty)<br />Socket.IO – A websockets Library<br />
  20. Links<br />http://github.com/sudar/jsfoo (all code samples used in this talk)<br />http://nodejs.org<br />http://npmjs.org<br />http://expressjs.com<br />http://socket.io<br />
  21. Questions<br />Thank you<br />Sudar Muthu<br />http://sudarmuthu.com<br />http://github.com/sudar<br />http://twitter.com/sudarmuthu<br />