2. Who
Developer for Kainos (Java, Scala, Rails, JavaScript, HTML,
CSS)
Student at University of Bath
Write a lot about JavaScript at
www.javascriptplayground.com
@Jack_Franklin on Twitter
3. Origins of Node
Server side JavaScript done right
runs on Chrome's V8 Engine (it's quick)
Evented I/O - runs single non-blocking thread with event
loop
this is great as JS was designed to be run in a single thread
environment (browser)
4. Node right now
V0.8 standardised the API (non breaking)
currently V0.8.4, much more stable than < 0.8
Install via installers, from source or via package manager like
Brew.
5. NPM
Package Manager for Node (think rubygems / bundler for
Node)
Full of really useful modules (and some less useful ones)
12,627 packages as of yesterday (browse at
http://search.npmjs.org/)
6. Event Driven JavaScript
Learn to think Asynchronously "Once node has completed a
task, the callback for it is ïŹred. But there can only be one
callback ïŹring at the same time. Until that callback has
ïŹnished executing, all other callbacks have to wait in line. In
addition to that, there is no guarantee on the order in which
the callbacks will ïŹre." From: http://debuggable.com/posts
/understanding-node-js:4bd98440-45e4-4a9a-
8ef7-0f7ecbdd56cb
Related: Async.js https://github.com/caolan/async/
7. Callbacks
Lots and lots of callbacks
var net = require('net');
var server = net.createServer(function (socket) {
socket.write('Echo serverrn');
socket.pipe(socket);
});
server.listen(1337, '127.0.0.1');
How to avoid: http://callbackhell.com/
8. Naming your Callbacks
Did you know you can name them?
var net = require('net');
var server = net.createServer(function writeResponse(socket) {
socket.write('Echo serverrn');
socket.pipe(socket);
});
server.listen(1337, '127.0.0.1');
9. Avoiding Callbacks: Modules
"Write small modules that each do one thing, and assemble
them into other modules that do a bigger thing. You can't get
into callback hell if you don't go there." Isaac Schlueter -
Node.js core contributor @izs
write your code in a ïŹle like normal:
//file socket-module.js
function writeResponse(socket) {
socket.write('Echo serverrn');
socket.pipe(socket);
});
10. module.exports = {
resp: writeResponse
}
(this follows the CommonJS module structure)
require and use it
var net = require('net');
var socketModule = require('socket-module');
var server = net.createServer(socketModule.resp);
server.listen(1337, '127.0.0.1');
12. Web Frameworks
Most popular is Express JS - www.expressjs.com
Loads out there - Google "node js framework"
Geddy, Flatiron, RailwayJS
Tools like this are slowly but surely maturing
13. Express
var app = express.createServer();
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen(3000);
routing, views (Jade), etc
very extensible
16. In the wild
JSBin by @rem - www.jsbin.com - pure awesomeness
TweetDig by @mheap - www.tweetdig.com - 2.5m tweets per
day
17. To Sum Up
Node is still very young, although standards and conventions
are beginning to be deïŹned.
Lack of resources is slowly becoming less of an issue.
V0.8 is huge improvement on prior versions.
Node is seriously quick if used properly.
18. Further Resources
@Peepcode screencasts www.peepcode.com
CodeSchool Node course www.codeschool.com
Async JavaScript book from Trevor Burnhan
www.leanpub.com/asyncjs
How to Node blog www.howtonode.org/
19. Any Questions?
Slides on Github: gist.github.com/jackfranklin
www.javascriptplayground.com for JavaScript tutorials
(including Node)
@Jack_Franklin if you can put up with even more of me
rambling