21
An Introduction to Node.JS Sudar Muthu (@sudarmuthu) Research Engineer Yahoo Labs http://sudarmuthu.com

A slightly advanced introduction to node.js

Embed Size (px)

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-jsfoo

Citation preview

Page 1: A slightly advanced introduction to node.js

An Introduction to Node.JS

Sudar Muthu (@sudarmuthu)Research Engineer

Yahoo Labshttp://sudarmuthu.com

Page 2: A slightly advanced introduction to node.js

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

Page 3: A slightly advanced introduction to node.js

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.

Page 4: A slightly advanced introduction to node.js

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.

Page 5: A slightly advanced introduction to node.js

Node.JS is not …

Ruby on RailsDjango Codeigniter

Node.JS is bare bone and the community are making stuff like Express, connect etc.

Page 6: A slightly advanced introduction to node.js

Why Node.JS?

Code like this

var result = db.query("select..");// use result

either blocks the entire process orimplies multiple execution stacks

(threads).

Page 7: A slightly advanced introduction to node.js

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.

Page 8: A slightly advanced introduction to node.js

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

Page 9: A slightly advanced introduction to node.js

Using Node.JSInstall Node.JSInstall NPMInstall other modules you want

by doing npm install <module_name>

You are good to go

Page 10: A slightly advanced introduction to node.js

Node.JS – as an interactive shell

Similar to Python’s shell

$> node> 3 + 14> true != falsetrue>.help>.exit

Page 11: A slightly advanced introduction to node.js

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

Page 12: A slightly advanced introduction to node.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

Page 13: A slightly advanced introduction to node.js

Core Modules

ProcessesFilesystemNetworkingUtilities

The entire list can be found at http://nodejs.org/docs/v0.4.12/api/

Page 14: A slightly advanced introduction to node.js

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

Page 15: A slightly advanced introduction to node.js

Core Modules - Filesystem

Low level API to manipulate files

Available Modules

fspath

Code Samples: http://github.com/sudar/jsfoo/filesystem.js

Page 16: A slightly advanced introduction to node.js

Core Modules - NetworkingAvailable Modules

netdgramhttptlshttpsdns

Code Samples: https://github.com/sudar/jsfoo/blob/master/dns.js

Page 17: A slightly advanced introduction to node.js

Core Modules - Utilities

Provides utility methods

Available Modules

consoleutil

Code Samples: https://github.com/sudar/jsfoo/blob/master/console.js

Page 18: A slightly advanced introduction to node.js

Node.JS is useful for..Writing highly concurrent server

applicationsSharing application logic between

server and clientPeer-to-peer web applications

using websockets

Page 19: A slightly advanced introduction to node.js

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

Page 20: A slightly advanced introduction to node.js

Linkshttp://github.com/sudar/jsfoo (all

code samples used in this talk)http://nodejs.orghttp://npmjs.orghttp://expressjs.comhttp://socket.io

Page 21: A slightly advanced introduction to node.js

Questions

Thank you

Sudar Muthuhttp://sudarmuthu.comhttp://github.com/sudarhttp://twitter.com/sudarmuthu