Introduction Node.js

39
Introduction Node.js ERIK VAN APPELDOORN | MARCH 28TH 2014 | SDN EVENT

description

Introduction to software development with Node.js

Transcript of Introduction Node.js

Page 1: Introduction Node.js

Introduction Node.jsERIK VAN APPELDOORN | MARCH 28TH 2014 | SDN EVENT

Page 2: Introduction Node.js

About me

Erik van Appeldoorn Trainer | Consultant Avans Hogeschool, Academie Industrie & Informatica

Twitter: @evappeld Blog: codeagainstthecode.tumblr.com Mail: [email protected]

Page 3: Introduction Node.js

Subjects

Page 4: Introduction Node.js

JavaScript WHY?

Unstructured Not intuitive Unlogical Prototypical Inheritance… WTF? Dynamically typed No Intellisense/code completion Hard to debug Global variables

Page 5: Introduction Node.js

JavaScript is strange (I)

console.log(1 == true); //trueconsole.log(1 === true); //falseconsole.log("0" == false); //trueconsole.log("abc" == "a" + "b" + "c"); //trueconsole.log(null == undefined); //trueconsole.log(30 -"7"); //23console.log("30" + 7); // 307

Page 6: Introduction Node.js

JavaScript is strange (II)

function rectangle(x,y) { this.x = x; this.y = y;}

rectangle.prototype.area = function() {return this.x * this.y;

}

var twoByThree = new rectangle(2,3);console.log(twoByThree.area());

Page 7: Introduction Node.js

But…JavaScript is relevant

THE language of the WEB Cross-platform Huge REACH JavaScript is everywhere (client, server, libs, frameworks) Single Page Applications Server applications End-to-End JavaScript applications

Page 8: Introduction Node.js

Learn JavaScript!

Page 9: Introduction Node.js

JavaScript the good parts

http://www.youtube.com/watch?v=hQVTIJBZook

Dough Crockford

Page 10: Introduction Node.js

Learning JavaScript

http://www.codecademy.com/tracks/javascript

http://www.w3schools.com/js/default.asp

http://eloquentjavascript.net/

Learn Node.js

Page 11: Introduction Node.js

What is Node.js? Open Source, Cross platform Development platform

Uses V8 JavaScript engine (Google)

“JavaScript on the server”

Node.js is a command line tool

Page 12: Introduction Node.js

Node.js history Developed in 2009

Published in 2011 (Linux only)

Ryan Dahl, Joyent

From July 2011 Windows version available

Active community: check http://nodeconf.com/

Page 13: Introduction Node.js

Node.js architecture Node.js is event-driven with Non-blocking I/O

Uses a Event loop for Non-blocking I/O

Node.js is single threaded

Page 14: Introduction Node.js

Node.js Event loop

Page 15: Introduction Node.js

Node.js applications

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

HTTP server REST server Web Sockets TCP server Utillities

Building Scalable Network programs

Page 16: Introduction Node.js

Hello Node.js World!

Page 17: Introduction Node.js

Hello Node.js World! + http server

Page 18: Introduction Node.js

http://nodejs.org/download/

Page 19: Introduction Node.js

Text editor JetBrains WebStorm 

Sublime Text

Visual Studio + Node.js tools for Visual Studio

Nodeclipse

Cloud9

Page 20: Introduction Node.js

Node Package Manager (NPM) www.npmjs.org

60.000+ modules

package.json file (npm init)

npm install <module.name> --save

DEMO

Page 21: Introduction Node.js

Polular NPM modules

express mocharestify jadesocket.io redisunderscore mongoosemoment requestasync connectgrunt passport

Page 22: Introduction Node.js

Local modules

DEMO

Page 23: Introduction Node.js

Node.js Website with express + jade

Page 24: Introduction Node.js

Node.js Website with express + jade Express: http://expressjs.com/ 

Connect: http://www.senchalabs.org/connect/

Jade: http://jade-lang.com/

DEMO Connect

Page 25: Introduction Node.js

Features express: Routing

Page 26: Introduction Node.js

Features express: Views

DEMO Express + Jade

Page 27: Introduction Node.js

Node.js REST API with Restify

Page 28: Introduction Node.js

Restify Maps HTTP verbs to CRUD operation 

Handles URL & Body params

RESTIFY: http://mcavage.me/node-restify/

Page 29: Introduction Node.js

Restify

DEMO Restify

Page 30: Introduction Node.js

WebSockets

Page 31: Introduction Node.js

Node.js WebSockets with Socket.io

Page 32: Introduction Node.js

Socket.io Bi-directional communication 

WebSockets + fallbacks

SOCKET.IO: http://socket.io/

Page 33: Introduction Node.js

Socket.io

DEMO Socket.io

Server

Client

Page 34: Introduction Node.js

Node.js with Mongoose & MongoDB

Page 35: Introduction Node.js

MongoDB NoSQL document store 

JSON collections

MongoDB: http://www.mongodb.org/

Robomongo: http://robomongo.org/

Page 36: Introduction Node.js

Mongoose Object modelling for MongoDB

Define schemas in Node.js for MongoDB

Query building (ODM)

Mongoose: http://mongoosejs.com/

Page 37: Introduction Node.js

Mongoose

DEMO Mongoose & MongoDB

Page 38: Introduction Node.js

The MEAN stack

http://www.mean.io

Page 39: Introduction Node.js

QUESTIONS?