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

0 %
100 %
Information about Node.js: The What, The How and The When

Published on March 5, 2014

Author: fitc_slideshare



Presented live at FITC Amsterdam 2014 on Feb 24-25, 2014
More details can be found at

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

Node.js Awesome, Lightweight, JavaScript, High Performance

WHO AM I? • Richard Nieuwenhuis • 32 years old • MediaMonks • 7 years a Monk • Lead of Operations / Head of development

Overview • What is Node.js • Some small examples • Asynchrony here, asynchrony there • Some (popular) Node.js modules • • High performance examples • Some tips & tricks

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

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.”

In short how many people see it • Server side JavaScript!

High Level Component overview Node.js V8 Event Loop (Libuv)

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)

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

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

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 :-)

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

Requiring needed modules //require the needed modules var http = require('http'); var fs = require('fs'); var sorter = require('./sorterservice');

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"); }

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); }

One last thing & Does it work? function sendFileNames(res, html){ res.setHeader('Content-Type', 'text/html'); res.end(html); }

Two big things not OK here Which are also two important Node aspects 1) Error handling 2) A-synchronous architecture

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); }

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

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)

“Problemo” numero dos fs.readdirSync(dirName); Node is a single threaded running process.

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

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);

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

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

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){……

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

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

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

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! 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

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]); }

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); });

Event Listener socket.on('message', function (message)…… socket.emit(„fired‟, {message: yep, you are fired});

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" }

ADIDAS NITROCHARGE An interactive cinematic experience using your mobile phone as a controller. 
Produced for DDB & Tribal Case Video

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

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

THANK YOU! • We are looking for new awesome developers! • Questions:

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages


© 2016 Node.js Foundation. All Rights Reserved. Portions of this site originally © 2016 Joyent. Node.js is a trademark of Joyent, Inc. and is used with ...
Read more

Node.js – Wikipedia

Node.js enthält einige Module, die direkt in das Binärpaket kompiliert wurden. Dazu gehören neben dem Modul für asynchronen Netzwerkzugriff auch ...
Read more

Node.js: Das JavaScript-Framework im Überblick | t3n

Node.js. Eine konkrete Implementierung von JavaScript auf dem Server ist Node.js [1], das 2009 von Ryan Dahl ins Leben gerufen wurde und seit dieser Zeit ...
Read more

Download | Node.js

Downloads. Latest LTS Version: ... Node.js is a trademark of Joyent, Inc. and is used with its permission. Please review the Trademark Guidelines of the ...
Read more

PHP oder Node.js? – Vorteile und Nachteile | NodeCode

... wofür man PHP und wofür Node.js verwenden sollte. Übersicht # Vorab aber eine grobe Übersicht, mit den wichtigsten Vorteilen beider Technologien. ...
Read more | Infos und Tutorials zu Node.js

Endlich ist es soweit: Das bereits angekündigte deutschsprachige Forum rund um Node.js ist ab sofort online, und unter der Domain aufrufbar.
Read more

Node.js Schulung Training Seminar - GROSSWEBER

Grenzen von Node.js (Überskalierung) und Lösung mit Proxys und Datenbanken wie Redis; Funktionale Programmierung mit Underscore.js;
Read more

Node.js -

Etwa ein Jahr nach dem Neustart von Node.js läuft das Projekt zumindest aus Sicht der Nutzer wieder rund.
Read more

Node.js Developer Center | Microsoft Azure

Erfahren Sie, wie Sie mit Microsoft Azure Ihre erste Node.js-Anwendung erstellen und auf Node.js-Tutorials und -Dokumentation zugreifen.
Read more

Node.js. Das umfassende Handbuch von Sebastian Springer

Durch diesen Aufbau eignet sich das Buch sowohl zum Erlernen von Node.js als auch als Nachschlagewerk im täglichen Gebrauch. Professionelle, ...
Read more