Wednesday, 2 January 2013

node.js an introduction and tutorial to javascript on the server


I gave a presentation on node.js on my first day back from the Christmas holidays. It was fun, plenty of people in the room, all eager to learn the basics of node.

Firstly an overview of what node is, what its good for and an introduction to the event driven architecture behind node. I did the classic fast food example and then a coding session.

The live coding session consisted of an introduction to the node REPL, some basic examples of javascript on the server, followed by a simple web server and some performance testing with apache bench (

To finish up I demoed a reverse proxy written in one line of node. It's amazing how much power this has, and even more amazing that you can write something like that in such a concise manner but which is still understandable (read maintainable).

I thought it went really well. I don't give many presentations but when I do I like them to be good, relevant, interesting and entertaining (as far as a technical subject can be). Of course I was nervous, especially because i was videoing it. I wanted to actually see what i was like. It's the best way to improve, fast feedback reflection and improvement.

Live coding is always dangerous but it all went remarkably well. Although I did have a small hiccup in that i could not connect to the wireless network, but that only impacted one of my examples, I weathered that storm.


So yes I videoed it and have uploaded to youtube Check it out I'd be really interested to hear your feedback on my presentation style and content.

Attached below are the code examples from the talk so you can test them out if you like.


Code demo


var add = function(a,b){return a+b};


fs.readFile('foo.txt', 'utf8', function (err,data) {

foo bar code

}, 2000);

}, 1000);

hello world web server

var http = require('http');
var server = http.createServer(function (req, res) {
res.end('Hello World\n');
server.listen(1337, '');
console.log('Server running at');

curl http://localhost:1337/
curl -i http://localhost:1337/

setTimeout(function() {
}, 6000);

The following needs apache bench installed and on your path
ab -n 10 -c 10

new file express.js

var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send("Welcome to node!");

node package manager

npm install express

app.get('/foo/', function(req, res){
res.send("Welcome to foo!");

a one line reverse proxy...

using request, a module to simplify the making of web requests
var http=require("http"),
http.createServer(function (req, res) {
req.pipe(request("" + req.url)).pipe(res);