Node.js for Pros
1 Introduction
▪ Robert Prediger
▪ 27 years experience in PROGRESS ▪ 20 years experience in web development ▪ 10 years experience in Node.js
Node.js for Pros 2 Node.js
▪ Javascript is everywhere: – Browsers – Webservers – Databases – Mobile Devices
▪ Adobe, Google, Apple and Microsoft are spending a huge amount of money!
▪ JavaScript seems to be the worlds most used programming language.
Node.js for Pros 3 Typescript (http://www.typescriptlang.org/)
interface Person { firstname: string; lastname: string; }
class Student { fullname : string; constructor( person : Person ) { this.fullname = person.firstname + " " + person.lastname; } }
var user = new Student( { firstname: "Jane", lastname: "User" } );
Node.js for Pros 4 Node.js
▪ What is Node.js – Server side Javascript – Built on Chrome V8 Engine – Event-driven, non blocking I/O model ▪ What is it for? – Easily building fast, scalable network applications – Perfect for data-intensive real-time application that run across distributed devices
Node.js for Pros 5 Node.js
One World One WAIT-FOR
Node.js for Pros 6 Node.js
▪ Request (other languages):
Finished request
Start 2nd Green is executing thread request
Red is waiting on I/O
Node.js for Pros 7 Node.js
▪ Banana slug ▪ F-18 Hornet – Max speed of – Max speed of 0.007 mph 1,190 mph ▪ Hard disk ▪ RAM
Quelle: http://blog.scoutapp.com/articles/2011/02/10/understanding-disk-i-o-when-should-you-be-worried Node.js for Pros 8 Node.js
Node.js for Pros 9 Node.js
Q: How is it possible to handle parallel I/O with one thread of execution?
A: There is (usually) no such thing as parallel I/O
Node.js for Pros 10 Node.js
▪ Parallel I/O: how we see it
Node.js for Pros 11 Node.js
▪ Parallel I/O: what actually happen at low level
Node.js for Pros 12 Node.js
Node.js for Pros 13 Node.js
▪ Request with Node:
Event Loop Request Request
Green is executing thread Red is waiting on I/O
Node.js for Pros 14 Nginx
Quelle: http://www.theorganicagency.com/apache-vs-nginx-performance-comparison/
Node.js for Pros 15 Nginx
Quelle: http://blog.webfaction.com/2008/12/a-little-holiday-present-10000-reqssec-with-nginx-2/ Node.js for Pros 16 Node.js
Node.js for Pros 17 Node.js
Can handle thousands of concurrent connections with minimal overhead (CPU/Memory) on a single Process!
Node.js for Pros 18 Modules
▪ Number of npm modules exceeded 1.000.000! ▪ Modules for nearly every problem: – pm2 – koa – socket.io – lodash – debug – moment – async – pdfmake
Node.js for Pros 19 pm2 PM2 is a production process manager for Node.js applications with a built-in load balancer.
It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
http://pm2.keymetrics.io/
Node.js for Pros 20 koa
▪ https://koajs.com/
const Koa = require('koa'), app = new Koa(), server = require('http').createServer( app.callback() ), koaBody = require('koa-body'), { userAgent } = require('koa-useragent');
app.use( koaBody() ); app.use( userAgent );
app.post( '/login', (ctx, next) => { ctx.body = 'Hello World'; });
app.listen( 8081 );
Node.js for Pros 21 koa-useragent
Node.js for Pros 22 socket.io
Protocol for having a bidirectional communication with client.
Node.js for Pros 23 socket.io
▪ http://socket.io/
let io = require('socket.io').listen( 80 );
io.sockets.on('connection', function (socket) { socket.emit( 'message', { text: 'Hello World' });
// broadcast message to all subscribed sockets socket.broadcast.emit("chat", { name: socket.nickname, msg: msg }); });
Node.js for Pros 24 socket.io
chat.node4biz.com
Node.js for Pros 25 lodash
▪ https://lodash.com
_.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x'); // => [{ 'x': 1 }, { 'x': 2 }]
let array = [ { 'dir': 'left', 'code': 97 }, { 'dir': 'right', 'code': 100 } ];
_.keyBy(array, 'dir'); // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
Node.js for Pros 26 debug
▪ https://github.com/visionmedia/debug
let debug = require("debug"), log = debug("demo:socket");
function login ( name ) { log( "login", name ); client.nickname = name; }
Node.js for Pros 27 moment
▪ https://momentjs.com/
moment("20111031", "YYYYMMDD").fromNow(); // 8 years ago moment().startOf('day').fromNow(); // 11 hours ago moment().endOf('day').fromNow(); // in 13 hours
moment().subtract(10, 'days').calendar(); // 10/21/2019
moment('2010-10-20').isBefore('2010-12-31', 'year'); // false
moment().add(7, 'days').subtract(1, 'months').year(2009).hours(0).minutes(0).seconds(0);
moment('2016-03-12 13:00:00').add(1, 'day').format('LLL') // "March 13, 2016 1:00 PM" moment('2016-03-12 13:00:00').add(24, 'hours').format('LLL') // "March 13, 2016 2:00 PM"
moment('2016-01-01').add(1, 'year').format('LL') // "January 1, 2017" moment('2016-01-01').add(365, 'day').format('LL') // "December 31, 2016"
Node.js for Pros 28 async
▪ http://caolan.github.io/async/v3/
// limits the number of concurrent requests async.eachLimit( customers, 10, (callback) => { ... callback( null ); }, function( err ) { // ends when all entries are done });
Node.js for Pros 29 pdfmake
▪ http://pdfmake.org
Node.js for Pros 30 Questions
Questions?
Node.js for Pros 31