All aboard the NodeJS Express

10,933 views

Published on

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.

Published in: Technology
1 Comment
8 Likes
Statistics
Notes
No Downloads
Views
Total views
10,933
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
65
Comments
1
Likes
8
Embeds 0
No embeds

No notes for slide

All aboard the NodeJS Express

  1. 1. All aboard the Node.JS Express #SotR14
  2. 2. You know Node? Kev McCabe is covering TDD SotR14 - Mura Room
  3. 3. Who is this guy? David Boyer Senior Software Developer NHS Wales Informatics Service
  4. 4. ...and yes... that's me in the mask @ SotR13
  5. 5. Node.js A JavaScript runtime http://nodejs.org
  6. 6. The Details JavaScript Google's V8 engine Open Source - MIT License Cross platform Single threaded code Multi threaded IO
  7. 7. What can it do? Build web applications Creating command line tools Create desktop applications (node-webkit)
  8. 8. Who is using it? PayPal, LinkedIn, Yahoo, Microsoft, eBay, 37signals, LearnBoost, Yammer, Walmart
  9. 9. Node in detail
  10. 10. In the beginning... Install Command line
  11. 11. Action!* node console.log('Hello Scotch');
  12. 12. Using the API var fs = require('fs'); // Filesystem API var http = require('http'); // http API var crypto = require('crypto'); // Crypto API
  13. 13. One thread to rule them all! One thread for JavaScript Multiple threads for I/O
  14. 14. The Event Loop Event Queue Single js thread Multiple I/O Threads    
  15. 15. 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);
  16. 16. Why not just be multi- threaded? Each thread needs more memory Switching between threads costs CPU Avoids thread safety issues
  17. 17. Scaling single threads The Cluster API ↗  ⇶  →  ↘ 
  18. 18. 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 }
  19. 19. Switching mindset From sync to async Lots of callbacks Callback Hell!
  20. 20. 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'); }) }) })
  21. 21. Escape from Nested Callbacks Break it apart Use a async module like "async" Promises Generators (ES6)
  22. 22. Node as a web application var http = require('http'); var server = http.createServer(function(req, res) res.end('Hello Scotch!'); }); server.listen(80);
  23. 23. 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());
  24. 24. npm The Node.js Package Manager
  25. 25. 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
  26. 26. Finding modules https://npmjs.org https://nodejsmodules.org/ http://eirikb.github.io/nipster/
  27. 27. Common modules Databases: mysql, mongodb, sqlite Callbacks: async, q, when, co Testing: Mocha, nodeunit, should Templating: Jade, handlebars, ejs Web frameworks: express,
  28. 28. Installing modules npm install async npm install --save coffee-script npm install --save-dev grunt-imagemin npm install npm uninstall --save coffee-script
  29. 29. Installing commands npm install -g coffee-script npm uninstall -g grunt-cli npm install -g gulp
  30. 30. Other npm super powers Publish modules Execute scripts (e.g. npm build, npm run) Bump your version number (semver)
  31. 31. 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
  32. 32. Express yourself Express, a web framework for Node.js Very flexible Minimal
  33. 33. 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.
  34. 34. Installing mkdir website cd website npm init npm install --save express touch index.js
  35. 35. A static site Serving static files from a directory
  36. 36. A dynamic site JavaScript providing the content
  37. 37. A template engine https://github.com/visionmedia/consoli Jade, EJS (Embedded JavaScript), Handlebars...
  38. 38. Try being MEAN!  Popular stack for building Node.js based web apps. MongoDB E xpress AngularJS Node.js
  39. 39. Thanks for listening to my first ever conference talk
  40. 40. Questions  If we have time... ...or catch me at the bar .
  41. 41. GAME  OVER All aboard the NodeJS Express David "Mister Dai" Boyer  @misterdai

×