A slightly advanced introduction to node.js
Upcoming SlideShare
Loading in...5
×
 

A slightly advanced introduction to node.js

on

  • 11,065 views

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

Statistics

Views

Total Views
11,065
Views on SlideShare
9,586
Embed Views
1,479

Actions

Likes
5
Downloads
154
Comments
1

10 Embeds 1,479

http://sudarmuthu.com 948
http://funnel.hasgeek.com 206
http://feeds.feedburner.com 150
http://feeds.feedburner.com 150
https://funnel.hasgeek.com 13
http://paper.li 8
http://www.onlydoo.com 1
http://a0.twimg.com 1
http://www.linkedin.com 1
http://localhost 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • I like, but all de links from github are broken. Please, adjust it! tks ;-)
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

A slightly advanced introduction to node.js A slightly advanced introduction to node.js Presentation Transcript

  • An Introduction to Node.JS
    Sudar Muthu (@sudarmuthu)
    Research Engineer
    Yahoo Labs
    http://sudarmuthu.com
  • Agenda
    What Node.JS is
    What Node.JS is not
    Why Node.JS?
    Using Node.JS
    As a interactive shell
    As a Server
    As a Client
    Common Modules
    Terminologies
    Questions
  • What is Node.JS
    Provides Evented, non-blocking I/O
    Built on Google’s V8
    JavaScript Programming Environment
    Supports C/C++ based addons
    Supports CommonJS Module format
    Is fast .. infact very fast.
  • What is Node.JS
    Similar to
    EventMachine in Ruby
    Twisted in Python
    But provides Evented IO as part of the language construct itself and not as a library.
  • Node.JS is not …
    Ruby on Rails
    Django
    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 or
    implies 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 the
    event loop immediately. No more
    unnecessary threads.
  • Demo of Callback
    // execute the callback after 2 seconds
    setTimeout(function () {
    console.log("World!");
    }, 2000);
    // print in console
    console.log("Hello");
    https://github.com/sudar/jsfoo/blob/master/callback.js
  • Using Node.JS
    Install Node.JS
    Install NPM
    Install 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 + 1
    4
    > true != false
    true
    >.help
    >.exit
  • Node.JS – As a server
    var http = require('http'); // require the http module
    // create a server
    http.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 Worldn');
    }).listen(1337, "127.0.0.1"); // listen on port 1337
    // debug information
    console.log('Server running at http://127.0.0.1:1337/');
    https://github.com/sudar/jsfoo/blob/master/http-server.js
  • Node.JS – As a client
    var http = require('http'); // require the needed modules
    // make the request object
    var request = http.request({
    'host': 'sudarmuthu.com',
    'port': 80,
    'path': '/',
    'method': 'GET'
    });
    // assign callbacks
    request.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
    Processes
    Filesystem
    Networking
    Utilities
    The entire list can be found at http://nodejs.org/docs/v0.4.12/api/
  • Core Modules - Processes
    Node allows you to analyze your process and manage external process
    Available Modules
    process
    child_process
    Code samples: http://github.com/sudar/jsfoo/process.js
  • Core Modules - Filesystem
    Low level API to manipulate files
    Available Modules
    fs
    path
    Code Samples: http://github.com/sudar/jsfoo/filesystem.js
  • Core Modules - Networking
    Available Modules
    net
    dgram
    http
    tls
    https
    dns
    Code Samples: https://github.com/sudar/jsfoo/blob/master/dns.js
  • Core Modules - Utilities
    Provides utility methods
    Available Modules
    console
    util
    Code Samples: https://github.com/sudar/jsfoo/blob/master/console.js
  • Node.JS is useful for..
    Writing highly concurrent server applications
    Sharing application logic between server and client
    Peer-to-peer web applications using websockets
  • Terminologies
    NPM – Package manager (like apt-get)
    Modules – Plugins or add-ons for Node.JS
    Express – MVC framework (like RoR)
    Jade – Template Engine (like Smarty)
    Socket.IO – A websockets Library
  • Links
    http://github.com/sudar/jsfoo (all code samples used in this talk)
    http://nodejs.org
    http://npmjs.org
    http://expressjs.com
    http://socket.io
  • Questions
    Thank you
    Sudar Muthu
    http://sudarmuthu.com
    http://github.com/sudar
    http://twitter.com/sudarmuthu