all aboard the nodejs express
DESCRIPTION
Get on board the NodeJS Express as we take a journey through what makes NodeJS special. Server-side JavaScript that has an event loop for a heart, we'll delve into its single threaded nature and the advantages provided. From there we'll pass through the land of the Node Package Management tool, how to set up your own package and bring in useful 3rd party packages as dependencies. Our final destination is ExpressJS, a Sinatra inspired framework for NodeJS.TRANSCRIPT
![Page 1: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/1.jpg)
All aboard theNode.JSExpress#SotR14
![Page 2: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/2.jpg)
You know Node?Kev McCabe is covering TDD
SotR14 - Mura Room
![Page 3: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/3.jpg)
Who is this guy?David Boyer
Senior SoftwareDeveloper
NHS Wales InformaticsService
![Page 4: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/4.jpg)
...and yes... that'sme in the mask @
SotR13
![Page 5: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/5.jpg)
Node.jsA JavaScript runtime
http://nodejs.org
![Page 6: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/6.jpg)
The DetailsJavaScriptGoogle's V8 engineOpen Source - MIT LicenseCross platformSingle threaded codeMulti threaded IO
![Page 7: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/7.jpg)
What can it do?Build web applications
Creating command line toolsCreate desktop applications
(node-webkit)
![Page 8: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/8.jpg)
Who is using it?PayPal, LinkedIn, Yahoo, Microsoft,
eBay, 37signals, LearnBoost,Yammer, Walmart
![Page 9: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/9.jpg)
Node in detail
![Page 10: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/10.jpg)
In the beginning...Install
Command line
![Page 11: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/11.jpg)
Action!*nodeconsole.log('Hello Scotch');
![Page 12: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/13.jpg)
One thread to rulethem all!
One thread for JavaScriptMultiple threads for I/O
![Page 14: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/14.jpg)
The Event LoopEventQueue
Single js thread MultipleI/OThreads
![Page 15: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/17.jpg)
Scaling single threadsThe Cluster API
↗ ⇶ →
↘
![Page 18: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/19.jpg)
Switching mindsetFrom sync to async
Lots of callbacksCallback Hell!
![Page 20: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/21.jpg)
Escape from NestedCallbacksBreak it apart
Use a async module like"async"
PromisesGenerators (ES6)
![Page 22: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/24.jpg)
npmThe Node.js Package Manager
![Page 25: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/26.jpg)
Finding moduleshttps://npmjs.org
https://nodejsmodules.org/http://eirikb.github.io/nipster/
![Page 27: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/27.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/29.jpg)
Installing commandsnpm install -g coffee-scriptnpm uninstall -g grunt-clinpm install -g gulp
![Page 30: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/30.jpg)
Other npm superpowers
Publish modulesExecute scripts (e.g. npm build,
npm run)Bump your version number
(semver)
![Page 31: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/32.jpg)
Express yourselfExpress, a web framework for
Node.jsVery flexible
Minimal
![Page 33: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/33.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/34.jpg)
Installingmkdir websitecd websitenpm initnpm install --save expresstouch index.js
![Page 35: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/35.jpg)
A static siteServing static files from a
directory
![Page 36: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/36.jpg)
A dynamic siteJavaScript providing the content
![Page 37: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/37.jpg)
A template enginehttps://github.com/visionmedia/consolidate.jsJade, EJS (Embedded JavaScript),
Handlebars...
![Page 38: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/38.jpg)
Try being MEAN!
Popular stack for building Node.jsbased web apps.
MongoDBE xpressA ngularJSN ode.js
![Page 39: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/39.jpg)
Thanks forlistening to my
first everconference talk
![Page 40: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/40.jpg)
Questions
If we have time...
...or catch me at the bar .
![Page 41: All aboard the NodeJS Express](https://reader034.vdocuments.net/reader034/viewer/2022052618/554a08c2b4c905507a8b56fd/html5/thumbnails/41.jpg)
GAME OVERAll aboard the
NodeJS ExpressDavid "Mister Dai"
Boyer @misterdai