introduction to node.js

26
Introduction to node.js @stevenbeeckman - #iotbe #njugbe

Upload: steven-beeckman

Post on 01-Jul-2015

531 views

Category:

Software


0 download

DESCRIPTION

The slides I used at the joint meetup of the Internet of Things Group Belgium and Node.js User Group Belgium.

TRANSCRIPT

Page 1: Introduction to node.js

Introduction to node.js@stevenbeeckman - #iotbe #njugbe

Page 2: Introduction to node.js

What is node.js?

Page 3: Introduction to node.js
Page 4: Introduction to node.js
Page 5: Introduction to node.js

keywords

easyfast developmentscalableevent-drivennon-blocking I/O model (single thread!)

Page 6: Introduction to node.js

event-driven & non-blocking I/O

Avoid long calculations and synchronuous calls

sensor.on(“data”, function(data){

// do something with the data

});

// code belows continues to be executed

Page 7: Introduction to node.js

Used by

IBMWalmartYahooLinkedInPayPalMozillaWorldline e-payment services and many more

Page 8: Introduction to node.js
Page 9: Introduction to node.js

A micro node.js introWho already knows JavaScript?

Page 10: Introduction to node.js

Variables & types

var name = “Steven Beeckman”;var age = 30;var alive = true;var length_in_meters = 1.98;length_in_meters = “N/A”;

console.log(typeof length_in_meters);

Page 11: Introduction to node.js

Variables & typesvar addresses = new Array();

var main_address = new Object();

main_address.street = “Wolvertemsestwg”;

main_address.postalcode = 1785;

main_address.type = “Main Address”;

main_address.dropPost = new function(package){...};

addresses.push(main_address);

console.dir(addresses);

Page 12: Introduction to node.js

Operatorsvar x = 4;

x = x + 1; // x is 5

y = x % 2; // y is 1

x++; // x is 6

x--; // x is 5

x += 3; // x is 8

var aString = "The value of x is " + x;

Page 13: Introduction to node.js

Math objectMath.floor(myInt);

Math.ceil(myInt);

Math.min(x, y);

Math.random(); // random number between 0 and up to but not equal to 1

Math.PI;

Page 14: Introduction to node.js

Flow controlif (x > 0) {

// do something

} else {

// do something else

}

x == 5 ? doWhenFive() : doWhenNotFive();

Page 15: Introduction to node.js

Loopsfor (var i = 0; i < myArray.length; i++) {

console.dir(myArray[i]);

}

while (x < 10) {

console.log(x++);

}

Page 16: Introduction to node.js

Functionsfunction myIncrementFunction (x) {

return x + 1;

}

var result = myIncrementFunction(5); // 6

// functions are first class citizens in JS

var inc = new function(x){ return x+1;}

result = inc(7); // 8

Page 17: Introduction to node.js

Packages - NPM

Node Package Manager - http://npmjs.org Command-line toolnpm install --save express

--save adds package automatically as a dependency in your package.json file-g installs the package globally

Page 18: Introduction to node.js

Using packages

var express = require(“express”);

Page 19: Introduction to node.js

Handling events

var five = require(“johnny-five”);

var myBoard = new five.Board();myBoard.on("ready", function() { // code to be executed when the board is ready});

Page 20: Introduction to node.js

MEAN stack

MongoDB (NoSQL document store)Express.js (~Sinatra of Ruby on Rails)Angular.js (Google’s front-end JS framework)Node.js (duh)

Page 21: Introduction to node.js

SocketsReal-time push with socket.iovar io = require('socket.io')(80);

var cfg = require('./config.json');

var tw = require('node-tweet-stream')(cfg);

tw.track('socket.io');

tw.track('javascript');

tw.on('tweet', function(tweet){

io.emit('tweet', tweet);

});

Page 22: Introduction to node.js

SocketsReal-time push with socket.io<script src="/socket.io/socket.io.js"></script>

<script>

var socket = io();

</script>

io.on('connection', function(socket){

console.log('a user connected');

socket.on('tweet', function(){

console.log(tweet);

});

});

Page 23: Introduction to node.js

Community

Open-source open-source open-source

~100k packages in npm~100m downloads per week

Page 24: Introduction to node.js

Nodebots & nodecopter

http://nodecopter.com/

Page 25: Introduction to node.js

Questions?@stevenbeeckman #iotbe #njugbe