Download - All aboard the NodeJS Express

Transcript
Page 1: All aboard the NodeJS Express

All aboard theNode.JSExpress#SotR14

Page 2: All aboard the NodeJS Express

You know Node?Kev McCabe is covering TDD

SotR14 - Mura Room

Page 3: All aboard the NodeJS Express

Who is this guy?David Boyer

Senior SoftwareDeveloper

NHS Wales InformaticsService

Page 4: All aboard the NodeJS Express

...and yes... that'sme in the mask @

SotR13

Page 5: All aboard the NodeJS Express

Node.jsA JavaScript runtime

http://nodejs.org

Page 6: All aboard the NodeJS Express

The DetailsJavaScriptGoogle's V8 engineOpen Source - MIT LicenseCross platformSingle threaded codeMulti threaded IO

Page 7: All aboard the NodeJS Express

What can it do?Build web applications

Creating command line toolsCreate desktop applications

(node-webkit)

Page 8: All aboard the NodeJS Express

Who is using it?PayPal, LinkedIn, Yahoo, Microsoft,

eBay, 37signals, LearnBoost,Yammer, Walmart

Page 9: All aboard the NodeJS Express

Node in detail

Page 10: All aboard the NodeJS Express

In the beginning...Install

Command line

Page 11: All aboard the NodeJS Express

Action!*nodeconsole.log('Hello Scotch');

Page 12: All aboard the NodeJS Express

Using the APIvar fs = require('fs'); // Filesystem APIvar http = require('http'); // http APIvar crypto = require('crypto'); // Crypto API

Page 13: All aboard the NodeJS Express

One thread to rulethem all!

One thread for JavaScriptMultiple threads for I/O

Page 14: All aboard the NodeJS Express

The Event LoopEventQueue

Single js thread MultipleI/OThreads

Page 15: All aboard the NodeJS Express

What does the code looklike?

var fs = require('fs');var rawData = fs.readFileSync('sotr.json');var data = JSON.parse(rawData);console.log(new Date());console.log(data.message);

Page 16: All aboard the NodeJS Express

Why not just be multi-threaded?

Each thread needs morememory

Switching between threadscosts CPU

Avoids thread safety issues

Page 17: All aboard the NodeJS Express

Scaling single threadsThe Cluster API

↗ ⇶ →

Page 18: All aboard the NodeJS Express

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) {...}).listen(}

Page 19: All aboard the NodeJS Express

Switching mindsetFrom sync to async

Lots of callbacksCallback Hell!

Page 20: All aboard the NodeJS Express

Highway to Hellfs.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, function console.log('Finish job'); }) })})

Page 21: All aboard the NodeJS Express

Escape from NestedCallbacksBreak it apart

Use a async module like"async"

PromisesGenerators (ES6)

Page 22: All aboard the NodeJS Express

Node as a webapplication

var http = require('http');

var server = http.createServer(function(req, res) res.end('Hello Scotch!');});

server.listen(80);

Page 23: All aboard the NodeJS Express

Modules// conf.jsfunction Conference(name, year) { this.name = name; this.year = year;};Conference.prototype.getTitle = function() { return this.name + ' ' + this.year;};module.exports = Conference

// index.jsvar Conf = require(__dirname + '/conf.js');var sotr = new Conf('Scotch on the Rocks', 2014);console.log(sotr.getTitle());

Page 24: All aboard the NodeJS Express

npmThe Node.js Package Manager

Page 25: All aboard the NodeJS Express

Initialise yourproject

npm init

name: (conference) sotr-exampleversion: (0.0.0) 0.1.2description: An example projectentry point: (index.js) index.jstest command: grunt nodeunitgit repository: git://github.com/misterdai/isnota.git#masterkeywords: example, appauthor: David Boyerlicense: (BSD-2-Clause) MIT

Page 26: All aboard the NodeJS Express

Finding moduleshttps://npmjs.org

https://nodejsmodules.org/http://eirikb.github.io/nipster/

Page 27: All aboard the NodeJS Express

Common modulesDatabases: mysql, mongodb,

sqliteCallbacks: async, q, when, co

Testing: Mocha, nodeunit,should

Templating: Jade, handlebars,ejs

Web frameworks: express,geddy, sails

Page 28: All aboard the NodeJS Express

Installing modulesnpm install asyncnpm install --save coffee-scriptnpm install --save-dev grunt-imagemin

npm install

npm uninstall --save coffee-script

Page 29: All aboard the NodeJS Express

Installing commandsnpm install -g coffee-scriptnpm uninstall -g grunt-clinpm install -g gulp

Page 30: All aboard the NodeJS Express

Other npm superpowers

Publish modulesExecute scripts (e.g. npm build,

npm run)Bump your version number

(semver)

Page 31: All aboard the NodeJS Express

SummaryLet npm manage your modules

Use it to install useful toolssuch as grunt

Don't have to keepnode_modules in source

controlStore project related

commands in package.json

Page 32: All aboard the NodeJS Express

Express yourselfExpress, a web framework for

Node.jsVery flexible

Minimal

Page 33: All aboard the NodeJS Express

Version 4Bundled middleware are nowtheir own modules*.* Except "static".More robust routing.app.router() is gone andmore flexible methods are nowavailable.

Page 34: All aboard the NodeJS Express

Installingmkdir websitecd websitenpm initnpm install --save expresstouch index.js

Page 35: All aboard the NodeJS Express

A static siteServing static files from a

directory

Page 36: All aboard the NodeJS Express

A dynamic siteJavaScript providing the content

Page 37: All aboard the NodeJS Express

A template enginehttps://github.com/visionmedia/consolidate.jsJade, EJS (Embedded JavaScript),

Handlebars...

Page 38: All aboard the NodeJS Express

Try being MEAN!

Popular stack for building Node.jsbased web apps.

MongoDBE xpressA ngularJSN ode.js

Page 39: All aboard the NodeJS Express

Thanks forlistening to my

first everconference talk

Page 40: All aboard the NodeJS Express

Questions

If we have time...

...or catch me at the bar .

Page 41: All aboard the NodeJS Express

GAME OVERAll aboard the

NodeJS ExpressDavid "Mister Dai"

Boyer @misterdai