KEMBAR78
Intro to Node.js
Introducing Node.js
About Your Speaker
What Is It?

Server side javascript framework

Written using Google V8 Engine

Uses CommonJS module system

Has the latest and greatest Ecmascript5
features
Non-Blocking I/O

The philosophy behind node.js is that system
interactions should be non-blocking

Instead of this:
var data = fs.readFile('foo.txt')
console.log(data);

This:
fs.readFile('foo.txt', function(data){
console.log(data);
});

This whole operation is asynchronous... my
application doesn't have to wait for the file to be
read in

Using javascript to do this makes it easier to
adopt given the existing knowledge of using
callbacks and listeners on the client side
Event Driven Architecture

This leads to a more event driven approach

Rather than returning something, calls should
try to either
− Call a callback passed in
− Fire events that the caller listens for

Built in event system (EventEmitter)
− emitter.emit('data-recvd', evt)
− emitter.on('data-recvd', function(evt){
});
Ecmascript 5 Features
Ecmascript 5 Features

Array Extras
− Map, reduce, forEach, filter, every, some

JSON

Object Utilities

Object.getOwnPropertyNames

Object.create

Object.defineProperties

Object.getOwnPropertyDescriptor

Immutibility
− Object.freeze, Object.seal
Ecmascript 5 Features

Getters/setters

Function.prototype.bind
− Allows you to create a new function with the
argument(s) pre-set
− Example
function add(a,b){
return a+b;
}
var fivePlus = add.bind(5)
console.log(fivePlus(2)); // prints 7
Examples

Enough talking, let's see some examples
− Some file and process interaction
− A simple webserver
− Reverse proxy
− Websockets
− AMQP to integrate with existing applications
Getting Started

Runs on nix and cygwin (no windows support
yet)

Install the latest stable from nodejs.org
− Sorry, no installer. You'll need to build it
yourself

Try the commandline repl out. If all is well,
you're good to go!

Hack away
npm

Ruby has gem, node has npm (node package
manager)

This is a must have for node.js development
− http://npmjs.org

Quick Install:
− curl http://npmjs.org/install.sh | sh

Can also be used to budle up your application's
dependencies
Modules of Note

Some interesting modules to take note of and
try out
− Express – simple sintatra clone (a dime a
dozen these days)
− Vows – async BDD in javascript
− Socket.IO – websockets made easy
− Yui3 – adaption of YUI for node.js
− SEVERAL NoSQL modules

Riak, redis, couchDB, mongoDB, tokyo cabinet,
etc.
Hosting Options

Heroku (closed beta)

Joyent (open beta)

Nodjitsu (launching soon)

DIY (EC2, your own server, etc)
Cons

Evolving rapidly.
− It's not uncommon to see tutorials from
several months ago that no longer work

Not very cross platform (yet)

Lots of duplicate modules
− Not that this is a bad thing, but for example
there are TEN couchdb modules (because
couchdb wrappers are simple to make)

No clustering support yet
Clustering Support

No real support yet, but it's on the way!
− EventEmitters backed by websockets
− Another using Redis built-in publish/subscribe
mechanisms
− I've been hacking at EventEmitters backed by
AMQP
Users
Join the Community!

irc.freenode.net #node.js

Google Groups node.js

File bugs/suggest features

Use and Contribute!

Intro to Node.js

  • 1.
  • 2.
  • 3.
    What Is It?  Serverside javascript framework  Written using Google V8 Engine  Uses CommonJS module system  Has the latest and greatest Ecmascript5 features
  • 4.
    Non-Blocking I/O  The philosophybehind node.js is that system interactions should be non-blocking  Instead of this: var data = fs.readFile('foo.txt') console.log(data);  This: fs.readFile('foo.txt', function(data){ console.log(data); });
  • 5.
     This whole operationis asynchronous... my application doesn't have to wait for the file to be read in  Using javascript to do this makes it easier to adopt given the existing knowledge of using callbacks and listeners on the client side
  • 6.
    Event Driven Architecture  Thisleads to a more event driven approach  Rather than returning something, calls should try to either − Call a callback passed in − Fire events that the caller listens for  Built in event system (EventEmitter) − emitter.emit('data-recvd', evt) − emitter.on('data-recvd', function(evt){ });
  • 7.
  • 8.
    Ecmascript 5 Features  ArrayExtras − Map, reduce, forEach, filter, every, some  JSON  Object Utilities  Object.getOwnPropertyNames  Object.create  Object.defineProperties  Object.getOwnPropertyDescriptor  Immutibility − Object.freeze, Object.seal
  • 9.
    Ecmascript 5 Features  Getters/setters  Function.prototype.bind −Allows you to create a new function with the argument(s) pre-set − Example function add(a,b){ return a+b; } var fivePlus = add.bind(5) console.log(fivePlus(2)); // prints 7
  • 10.
    Examples  Enough talking, let'ssee some examples − Some file and process interaction − A simple webserver − Reverse proxy − Websockets − AMQP to integrate with existing applications
  • 11.
    Getting Started  Runs onnix and cygwin (no windows support yet)  Install the latest stable from nodejs.org − Sorry, no installer. You'll need to build it yourself  Try the commandline repl out. If all is well, you're good to go!  Hack away
  • 12.
    npm  Ruby has gem,node has npm (node package manager)  This is a must have for node.js development − http://npmjs.org  Quick Install: − curl http://npmjs.org/install.sh | sh  Can also be used to budle up your application's dependencies
  • 13.
    Modules of Note  Someinteresting modules to take note of and try out − Express – simple sintatra clone (a dime a dozen these days) − Vows – async BDD in javascript − Socket.IO – websockets made easy − Yui3 – adaption of YUI for node.js − SEVERAL NoSQL modules  Riak, redis, couchDB, mongoDB, tokyo cabinet, etc.
  • 14.
    Hosting Options  Heroku (closedbeta)  Joyent (open beta)  Nodjitsu (launching soon)  DIY (EC2, your own server, etc)
  • 15.
    Cons  Evolving rapidly. − It'snot uncommon to see tutorials from several months ago that no longer work  Not very cross platform (yet)  Lots of duplicate modules − Not that this is a bad thing, but for example there are TEN couchdb modules (because couchdb wrappers are simple to make)  No clustering support yet
  • 16.
    Clustering Support  No realsupport yet, but it's on the way! − EventEmitters backed by websockets − Another using Redis built-in publish/subscribe mechanisms − I've been hacking at EventEmitters backed by AMQP
  • 17.
  • 18.
    Join the Community!  irc.freenode.net#node.js  Google Groups node.js  File bugs/suggest features  Use and Contribute!