introduction to node.js
DESCRIPTION
The slides I used at the joint meetup of the Internet of Things Group Belgium and Node.js User Group Belgium.TRANSCRIPT
Introduction to node.js@stevenbeeckman - #iotbe #njugbe
What is node.js?
keywords
easyfast developmentscalableevent-drivennon-blocking I/O model (single thread!)
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
Used by
IBMWalmartYahooLinkedInPayPalMozillaWorldline e-payment services and many more
A micro node.js introWho already knows JavaScript?
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);
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);
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;
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;
Flow controlif (x > 0) {
// do something
} else {
// do something else
}
x == 5 ? doWhenFive() : doWhenNotFive();
Loopsfor (var i = 0; i < myArray.length; i++) {
console.dir(myArray[i]);
}
while (x < 10) {
console.log(x++);
}
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
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
Using packages
var express = require(“express”);
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});
MEAN stack
MongoDB (NoSQL document store)Express.js (~Sinatra of Ruby on Rails)Angular.js (Google’s front-end JS framework)Node.js (duh)
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);
});
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);
});
});
Community
Open-source open-source open-source
~100k packages in npm~100m downloads per week
Questions?@stevenbeeckman #iotbe #njugbe
Referenceshttp://nodejs.orghttp://expressjs.comhttp://nodeschool.iohttp://www.meetup.com/Belgian-node-js-User-Group/ http://node-ardx.org/js-primer https://www.npmjs.org/http://www.heroku.com http://www.digitalocean.comhttp://www.nodejitsu.com http://www.mongodb.com