mat@schaffer.me
@matschaffer
http://github.com/
matschaffer
http://mashion.net
didn’t write node.js
I’m not Ryan Dahl

but he’ll be at
Philly ETE
What is   ?
?
But why?
• Larger developer pool
• Write-once business logic
• Server-side fallbacks

 but also....
Asynchonous
     vs.
Synchronous
// Asynchronous
$.get('ajax/test.html',
      function(data) {
        $('.result').html(data);
        alert('Async!');
      });
// Synchronous
var data = $.get('ajax/test.html');
Evented (async)
      vs.
Threaded (sync)
Getting it (source)

git clone git://github.com/ry/node.git
cd node
./configure --prefix=/usr/local
make
make install || sudo make install
Getting it (packaged)

     brew install node


    port install nodejs
Node Basics
console.log('hello world');

$ node helloworld.js
hello world
var fs = require('fs');

fs.writeFile('hello.txt', 'hello world',
  function (err, fd) {
   if (err) throw err;
   console.log('done.');
  });

$ node helloworld.js
done.

$ cat hello.txt
hello world
var fs = require('fs');



• require(‘mylib’) ≈ <script src=”mylib.js”>
• require returns the library’s exports
module.exports = {
  writeFile: function(path, data, callback) {...},
  readFile: function(path, callback) {...}
};
A web server
var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200,
    {'Content-Type': 'text/plain'});
  res.end("Hello worldn");
}).listen(8000);
Working with Evented
     Libraries
not this...
things.each(function(thing) {
  doStuffWith(i);
});
this!
things.each(function(thing, next) {
  doStuffWith(i, next);
});
Get packages with npm


   npm install express
Testing Options

• Built in assert library
• Jasmine
• Expresso
• Vows
Cool Node.js Projects
node-inspector
Express
var express = require('express'),
    app = express.createServer();

app.get('/', function(req, res){
    res.send('Hello World');
});

app.listen(3000);
Socket.IO
         Real time client/server messaging

// socket.io
var socket = io.listen(server);
socket.on('connection', function(client){
  // new client is here!
  client.on('message', function(){ … })
  client.on('disconnect', function(){ … })
});
Massively multiplayer
  scrabble!!1!1!!
App in a browser!!
Analytics pr0n!11!!!!1

Node.js