Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Node.js: The What, The How and The When


Published on

Save 10% off ANY FITC event with discount code 'slideshare'
See our upcoming events at

Node.js: The What, The How and The When
with Richard Nieuwenhuis

Published in: Technology
  • Be the first to comment

Node.js: The What, The How and The When

  1. 1. Node.js Awesome, Lightweight, JavaScript, High Performance
  2. 2. WHO AM I? • Richard Nieuwenhuis • 32 years old • MediaMonks • 7 years a Monk • Lead of Operations / Head of development
  3. 3. Overview • What is Node.js • Some small examples • Asynchrony here, asynchrony there • Some (popular) Node.js modules • • High performance examples • Some tips & tricks
  4. 4. When and by whom? • First made public in 2009 • Ryan Lienhart Dahl • Currently at version 0.10.25* • “Why is a progress bar so difficult?” • • “Why the heck are we polling???” This sheet was created 17-2-2014
  5. 5. What says • “Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.”
  6. 6. In short how many people see it • Server side JavaScript!
  7. 7. High Level Component overview Node.js V8 Event Loop (Libuv)
  8. 8. Where to begin? • Download & Install Node.js • • Use your favorite IDE Node.js API/Modules: • • Sort of API‟s which expose Node functions • Allows the Node.js community to extend Node.js • • Built-in Super lightweight Add-ons • Quite a big community • The NPM (Node Package Manager)
  9. 9. Some Node.js APIs/Modules • http • • Provides basic functions for creating a server and handling requests https • • For setting up a secured server assert • • For creating unit tests for your code (NodeTDD!) fs • For doing stuff with the File System
  10. 10. Some Node.js APIs/Modules continued • querystring • • For parsing the query string of a request url • • functions for parsing and resolving URL‟s repl: Read-Eval-Print-Loop • Good for debugging and testing
  11. 11. Let‟s build something simple • Simple server • Every “Hello World” Example starts with this. • Read the files of a folder. • Print the file names in the browser in sorted fashion. • PS. Examples show Node specifics and are to illustrate points and are by no means “elegant, save” etc :-)
  12. 12. Create our own module: sorterservice.js function sortFileNames(fileNames){ return fileNames.sort(); } exports.sortFileNames = sortFileNames; - a module can be a single file a module can be a folder exports is a special object to define properties - Can be any type: function, strings, etc
  13. 13. Requiring needed modules //require the needed modules var http = require('http'); var fs = require('fs'); var sorter = require('./sorterservice');
  14. 14. Create server and verify requests http.createServer(function(req, res){ switch(req.method){ case 'POST': console.log('This is a post request'); break; case 'GET': switch(req.url){ case '/': readFileNames(res); break; case '/showfiles„: readFileNames(res); break; case '/favicon.ico„: break; } break; default: res.setHeader('Content-Type', 'text/plain'); res.end("This method I simply do not know"); }
  15. 15. Let‟s read the file names function readFileNames(res){ var dirName = './files'; var html = ''; var fileNames = fs.readdirSync(dirName); var sortedFileNames = sorter.sortFileNames(fileNames); html += '<ol>'; for(var index in sortedFileNames){ html += '<li>' + sortedFileNames[index] + '</li>'; } html += '</ol>'; sendFileNames(res, html); }
  16. 16. One last thing & Does it work? function sendFileNames(res, html){ res.setHeader('Content-Type', 'text/html'); res.end(html); }
  17. 17. Two big things not OK here Which are also two important Node aspects 1) Error handling 2) A-synchronous architecture
  18. 18. Main problem 1 function readFileNames(res){ var dirName = './files'; var html = ''; var fileNames = []; fileNames = fs.readdirSync(dirName); var sortedFileNames = sorter.sortFileNames(fileNames); html += '<ol>'; for(var index in sortedFileNames){ html += '<li>' + sortedFileNames[index] + '</li>'; } html += '</ol>'; sendFileNames(res, html); }
  19. 19. What did we see? • The server crashed and Node stopped running • A lot less forgiving than PHP for example • Not OK during a super bowl commercial where millions of Dollars are spend ;-) Tip: Look at the Node.js module forever
  20. 20. In this case a simple if (fs.existsSync(dirName)){ fileNames = fs.readdirSync(dirName); } Would suffice…. • Defensive programming is super important in Node • Verify everything! • Valid user input • Expect the unexpected (even if you expect otherwise) • Act on any error that may occur (and log it) • Have a process in place to restart node just to be sure (forever module)
  21. 21. “Problemo” numero dos fs.readdirSync(dirName); Node is a single threaded running process.
  22. 22. Synchronous development • Blocking/Synchronous requests are bad • • It will “block” the continuation of your logic for the amount of time the blocking request is active (requesting a JSON file for example) On the server you basically have the same problems • • But it will affect all visitors immediately when one request is stalling Can ultimately lead to a crashing server
  23. 23. Synchronous development http.createServer(function(req, res){ switch(req.method){ case 'POST': console.log('This is a post request'); break; case 'GET': //”Select *” all 200K users from the database //Give it back to the response } }).listen(8888);
  24. 24. Continued Pffff, I need to return 200K rows? Request Cpu to r2: Sorry, I am doing nothing, but the DB is sooooo slowww Request 2 Going to take me a while! doQuery() CPU processData() Cpu time DB
  25. 25. The Event Loop Take this event loop!: doQuery(cb) Pffff, I need to return 200K rows? Going to take me a while! Request CPU to r2: thanks man! I am processing your request Request 2 CPU processData() E loop callback DB
  26. 26. A-Synchronous Node development A lot of Node functions have a-synchronous versions. fs.readdir(dirName, function(err, data){ if (err){ console.log(); } var sortedFileNames = sorter.sortFileNames(data); //The other code --------------------------http.createServer(function(req, res){……
  27. 27. A-Synchronous Node development • A-synchronize as much as possible to keep the main process as “free” as possible. • • Much more necessary when it is a high-performance environment It is not the holy grail! Keep the Big O in mind: for (var x = 0; x < 25.5Billion; x++) • Sequencing & parallel • Can become quite messy and unreadable
  28. 28. A highly unlikely hypothetical situation • Read the files from Dir A • Merge the content of all files of Dir A into one file • Read the files from Dir B • Append the content of step 1 to all files in Dir B • Store these results in the Database
  29. 29. Would look something like this aSynReadDir('A/', function(files) { mergeFiles(files, function(fileContents) { aSynReadDir('B/', function(files) { appendFiles(fileContents, files, function(appendedFiles) { insertIntoDatabase(appendedFiles, function() { }); }); }); }); });
  30. 30. Useful Node Modules: • Latest browsers support Web sockets • IE10+ • Chrome • FireFox • “Weirdly” on Android only starting from 4.4… • leverages the Web socket technology for bi-directional communication between clients & servers: • With degradability in place for the “older” browsers!
  31. 31. continued • Very nice for real time applications with a lot of concurrent users. • Browser based games where you mobile is the controller • Second screen applications • Updating the client: • Chat • Real time statistics • News updates • Etc
  32. 32. Small example (Server) var server = http.createServer(function(req, res){ //code here }); var io = socketio.listen(server); function sendRandomMessage(){ var index = Math.floor((Math.random()*4)); io.sockets.send(words[index]); }
  33. 33. Small example (Client) var socket = io.connect(); socket.on('message', function (message) { console.log(„I received the message ' + message); var m = document.createElement('p'); m.innerText = message; document.getElementById('text').appendChild(m); });
  34. 34. Event Listener socket.on('message', function (message)…… socket.emit(„fired‟, {message: yep, you are fired});
  35. 35. Other Useful Node Modules "dependencies": { "express": "3.4.7", // Web Framework "express-validator": "1.0.1", "request": "2.25.0", // "redis": "0.10.0", //Redis: High performance in memory DB "hiredis": "0.1.16", // To make Redis even faster with C written stuff "redis-sentinel": "0.0.5", // Redis failover "http-auth": "*", //for authentication "openid": "0.5.5", //Open ID client "statsd-client": "*", //NodeJS deamon for logging request s tatistics "facebook-node-sdk": "0.2.0", "librato-metrics": "0.0.7", "statsd-client": "0.0.15" }
  36. 36. ADIDAS NITROCHARGE An interactive cinematic experience using your mobile phone as a controller. 
Produced for DDB & Tribal Case Video
  37. 37. KLM CLAIM YOUR PLACE IN SPACE Are you ready to win the ultimate journey of inspiration? Claim your spot in space 
Produced for DDB & Tribal Amsterdam Case Video
  38. 38. THE VOICE OF HOLLAND Talpa This season you can be a coach at home. Show your coaching qualities with The Voice of Holland Thuiscoach app. Case Video
  39. 39. THANK YOU! • We are looking for new awesome developers! • Questions: