- 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
- 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)
- 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.
- 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/)
- Learn to think Asynchronously "Once node has completed a task, the callback for it is fired. But there can only be one callback firing at the same time. Until that callback has finished 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 fire." From: http://debuggable.com/posts/understanding-node-js:4bd98440-45e4-4a9a-8ef7-0f7ecbdd56cb
- Related: Async.js https://github.com/caolan/async/
## Callbacks
- Lots and lots of callbacks
var net = require('net');
var server = net.createServer(function (socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
server.listen(1337, '127.0.0.1');
- How to avoid: http://callbackhell.com/
- Did you know you can name them?
var net = require('net');
var server = net.createServer(function writeResponse(socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
server.listen(1337, '127.0.0.1');
-
"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 file like normal:
//file socket-module.js
function writeResponse(socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
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');
- Redis & the Redis NPM Package is awesome
- Adapters for all common DB solutions
- 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
var app = express.createServer();
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen(3000);
- routing, views (Jade), etc
- very extensible
- About to hit V3
- NodeUnit https://github.com/caolan/nodeunit
- Mocha http://visionmedia.github.com/mocha/
- Lots more. Find one that suits (I love Mocha)
- JSBin by @rem - www.jsbin.com - pure awesomeness
- TweetDig by @mheap - www.tweetdig.com - 2.5m tweets per day
- Node is still very young, although standards and conventions are beginning to be defined.
- 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.
- @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/
- 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