All aboard the
Node.JS
Express
#SotR14
You know Node?
Kev McCabe is covering TDD
SotR14 - Mura Room
Who is this guy?
David Boyer
Senior Software
Developer
NHS Wales Informatics
Service
...and yes... that's
me in the mask @
SotR13
Node.js
A JavaScript runtime
http://nodejs.org
The Details
JavaScript
Google's V8 engine
Open Source - MIT License
Cross platform
Single threaded code
Multi threaded IO
What can it do?
Build web applications
Creating command line tools
Create desktop applications
(node-webkit)
Who is using it?
PayPal, LinkedIn, Yahoo, Microsoft,
eBay, 37signals, LearnBoost,
Yammer, Walmart
Node in detail
In the beginning...
Install
Command line
Action!*
node
console.log('Hello Scotch');
Using the API
var fs = require('fs'); // Filesystem API
var http = require('http'); // http API
var crypto = require('crypto'); // Crypto API
One thread to rule
them all!
One thread for JavaScript
Multiple threads for I/O
The Event Loop
Event
Queue
Single js thread Multiple
I/O
Threads
  

What does the code look
like?
var fs = require('fs');
var rawData = fs.readFileSync('sotr.json');
var data = JSON.parse(rawData);
console.log(new Date());
console.log(data.message);
Why not just be multi-
threaded?
Each thread needs more
memory
Switching between threads
costs CPU
Avoids thread safety issues
Scaling single threads
The Cluster API
↗ 
⇶  → 
↘ 
var cluster = require('cluster');
var http = require('http');
var cpus = require('os').cpus().length;
if (cluster.isMaster) {
for (var i = 0; i < cpus; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal
console.log('Process ID ' + worker.process.pid
});
} else {
http.createServer(function(req, res) {...}).list
}
Switching mindset
From sync to async
Lots of callbacks
Callback Hell!
Highway to Hell
fs.readFile('sotr.json', function(err, data) {
var info = JSON.parse(data);
db.findOne({id: info.id}, function(err, record)
fs.writeFile('sotr.dat', record.title, functio
console.log('Finish job');
})
})
})
Escape from Nested
Callbacks
Break it apart
Use a async module like
"async"
Promises
Generators (ES6)
Node as a web
application
var http = require('http');
var server = http.createServer(function(req, res)
res.end('Hello Scotch!');
});
server.listen(80);
Modules
// conf.js
function Conference(name, year) {
this.name = name; this.year = year;
};
Conference.prototype.getTitle = function() {
return this.name + ' ' + this.year;
};
module.exports = Conference
// index.js
var Conf = require(__dirname + '/conf.js');
var sotr = new Conf('Scotch on the Rocks', 2014);
console.log(sotr.getTitle());
npm
The Node.js Package Manager
Initialise your
project
npm init
name: (conference) sotr-example
version: (0.0.0) 0.1.2
description: An example project
entry point: (index.js) index.js
test command: grunt nodeunit
git repository: git://github.com/misterdai/isnota.
keywords: example, app
author: David Boyer
license: (BSD-2-Clause) MIT
Finding modules
https://npmjs.org
https://nodejsmodules.org/
http://eirikb.github.io/nipster/
Common modules
Databases: mysql, mongodb,
sqlite
Callbacks: async, q, when, co
Testing: Mocha, nodeunit,
should
Templating: Jade, handlebars,
ejs
Web frameworks: express,
Installing modules
npm install async
npm install --save coffee-script
npm install --save-dev grunt-imagemin
npm install
npm uninstall --save coffee-script
Installing commands
npm install -g coffee-script
npm uninstall -g grunt-cli
npm install -g gulp
Other npm super
powers
Publish modules
Execute scripts (e.g. npm build,
npm run)
Bump your version number
(semver)
Summary
Let npm manage your modules
Use it to install useful tools
such as grunt
Don't have to keep
node_modules in source
control
Store project related
commands in package.json
Express yourself
Express, a web framework for
Node.js
Very flexible
Minimal
Version 4
Bundled middleware are now
their own modules*.
* Except "static".
More robust routing.
app.router()is gone and
more flexible methods are now
available.
Installing
mkdir website
cd website
npm init
npm install --save express
touch index.js
A static site
Serving static files from a
directory
A dynamic site
JavaScript providing the content
A template engine
https://github.com/visionmedia/consoli
Jade, EJS (Embedded JavaScript),
Handlebars...
Try being MEAN!

Popular stack for building Node.js
based web apps.
MongoDB
E xpress
AngularJS
Node.js
Thanks for
listening to my
first ever
conference talk
Questions

If we have time...
...or catch me at the bar .
GAME  OVER
All aboard the
NodeJS Express
David "Mister Dai"
Boyer
 @misterdai

All aboard the NodeJS Express