Your SlideShare is downloading. ×
0
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
A slightly advanced introduction to node.js
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

A slightly advanced introduction to node.js

10,530

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

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
5 Likes
Statistics
Notes
No Downloads
Views
Total Views
10,530
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
162
Comments
1
Likes
5
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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(&quot;select..&quot;);<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(&quot;select..&quot;, 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(&quot;World!&quot;);<br />}, 2000);<br />// print in console<br />console.log(&quot;Hello&quot;);<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 &lt;module_name&gt;<br />You are good to go<br />
  • 10. Node.JS – as an interactive shell<br />Similar to Python’s shell<br />$&gt; node<br />&gt; 3 + 1<br />4<br />&gt; true != false<br />true<br />&gt;.help<br />&gt;.exit<br />
  • 11. Node.JS – As a server<br />var http = require(&apos;http&apos;); // 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 /> &apos;Content-Type&apos;: &apos;text/plain&apos;<br /> });<br /> // send this as part of the response<br />res.end(&apos;Hello Worldn&apos;);<br />}).listen(1337, &quot;127.0.0.1&quot;); // listen on port 1337<br />// debug information<br />console.log(&apos;Server running at http://127.0.0.1:1337/&apos;);<br />https://github.com/sudar/jsfoo/blob/master/http-server.js<br />
  • 12. Node.JS – As a client<br />var http = require(&apos;http&apos;); // require the needed modules<br />// make the request object<br />var request = http.request({<br /> &apos;host&apos;: &apos;sudarmuthu.com&apos;,<br /> &apos;port&apos;: 80,<br /> &apos;path&apos;: &apos;/&apos;,<br /> &apos;method&apos;: &apos;GET&apos;<br />});<br />// assign callbacks<br />request.on(&apos;response&apos;, function (response) {<br />console.log(&apos;Response Status Code: &apos; + response.statusCode);<br />response.on(&apos;data&apos;, function (data) {<br />console.log(&apos;Body: &apos; + 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 />

×