a slightly advanced introduction to node.js
DESCRIPTION
Slides from my talk about node.js which I gave at jsFoo. More info at http://sudarmuthu.com/blog/introduction-to-node-js-at-jsfooTRANSCRIPT
An Introduction to Node.JS
Sudar Muthu (@sudarmuthu)Research Engineer
Yahoo Labshttp://sudarmuthu.com
AgendaWhat Node.JS isWhat Node.JS is notWhy Node.JS?Using Node.JS
◦As a interactive shell◦As a Server◦As a Client
Common ModulesTerminologiesQuestions
What is Node.JSProvides Evented, non-blocking
I/OBuilt on Google’s V8JavaScript Programming
EnvironmentSupports C/C++ based addonsSupports CommonJS Module
formatIs fast .. infact very fast.
What is Node.JS
Similar to
EventMachine in RubyTwisted in Python
But provides Evented IO as part of the language construct itself and not as a library.
Node.JS is not …
Ruby on RailsDjango Codeigniter
Node.JS is bare bone and the community are making stuff like Express, connect etc.
Why Node.JS?
Code like this
var result = db.query("select..");// use result
either blocks the entire process orimplies multiple execution stacks
(threads).
Why Node.JS?
But a line of code like this
db.query("select..", function (result) { // use result});
allows the program to return to theevent loop immediately. No moreunnecessary threads.
Demo of Callback// execute the callback after 2 secondssetTimeout(function () { console.log("World!");}, 2000);
// print in consoleconsole.log("Hello");
https://github.com/sudar/jsfoo/blob/master/callback.
js
Using Node.JSInstall Node.JSInstall NPMInstall other modules you want
by doing npm install <module_name>
You are good to go
Node.JS – as an interactive shell
Similar to Python’s shell
$> node> 3 + 14> true != falsetrue>.help>.exit
Node.JS – As a servervar http = require('http'); // require the http module
// create a serverhttp.createServer(function (req, res) { // call this function when a request is received res.writeHead(200, { 'Content-Type': 'text/plain' }); // send this as part of the response res.end('Hello World\n');}).listen(1337, "127.0.0.1"); // listen on port 1337
// debug informationconsole.log('Server running at http://127.0.0.1:1337/');
https://github.com/sudar/jsfoo/blob/master/http-server.js
Node.JS – As a clientvar http = require('http'); // require the needed modules
// make the request objectvar request = http.request({ 'host': 'sudarmuthu.com', 'port': 80, 'path': '/', 'method': 'GET'});
// assign callbacksrequest.on('response', function (response) { console.log('Response Status Code: ' + response.statusCode); response.on('data', function (data) { console.log('Body: ' + data); });});
https://github.com/sudar/jsfoo/blob/master/http-client.js
Core Modules
ProcessesFilesystemNetworkingUtilities
The entire list can be found at http://nodejs.org/docs/v0.4.12/api/
Core Modules - ProcessesNode allows you to analyze your
process and manage external process
Available Modules
processchild_process
Code samples: http://github.com/sudar/jsfoo/process.js
Core Modules - Filesystem
Low level API to manipulate files
Available Modules
fspath
Code Samples: http://github.com/sudar/jsfoo/filesystem.js
Core Modules - NetworkingAvailable Modules
netdgramhttptlshttpsdns
Code Samples: https://github.com/sudar/jsfoo/blob/master/dns.js
Core Modules - Utilities
Provides utility methods
Available Modules
consoleutil
Code Samples: https://github.com/sudar/jsfoo/blob/master/console.js
Node.JS is useful for..Writing highly concurrent server
applicationsSharing application logic between
server and clientPeer-to-peer web applications
using websockets
TerminologiesNPM – Package manager (like
apt-get)Modules – Plugins or add-ons for
Node.JSExpress – MVC framework (like
RoR)Jade – Template Engine (like
Smarty)Socket.IO – A websockets Library
Linkshttp://github.com/sudar/jsfoo (all
code samples used in this talk)http://nodejs.orghttp://npmjs.orghttp://expressjs.comhttp://socket.io
Questions
Thank you
Sudar Muthuhttp://sudarmuthu.comhttp://github.com/sudarhttp://twitter.com/sudarmuthu