node azure

66
AZ02 – NODE.JS SU WINDOWS AZURE #CDays13 – 27 e 28 febbraio 2013 Emanuele DelBono Software engineer @emadb

Upload: emanuele-delbono

Post on 15-Jan-2015

1.192 views

Category:

Technology


4 download

DESCRIPTION

an introduction to node.js and how to publish the apps on windows azure.

TRANSCRIPT

Page 1: Node azure

AZ02 – NODE.JS SU WINDOWS AZURE

#CDays13 – 27 e 28 febbraio 2013

Emanuele DelBonoSoftware engineer@emadb

Page 2: Node azure

Sponsor!razie a…

Page 3: Node azure

Do you really know javascript?

Page 4: Node azure
Page 5: Node azure

> [] + []

Page 6: Node azure

> [] + []

''

Page 7: Node azure
Page 8: Node azure

> [] + {}

Page 9: Node azure

> [] + {}

{}

Page 10: Node azure
Page 11: Node azure

> {} + []

Page 12: Node azure

> {} + []

0

Page 13: Node azure
Page 14: Node azure

> {} + {}

Page 15: Node azure

> {} + {}NaN

Page 16: Node azure
Page 17: Node azure

> Array(10)

Page 18: Node azure

> Array(10)

[ , , , , , , , , , , ]

Page 19: Node azure

> Array(10)

[ , , , , , , , , , , ]

> Array(10).join('yo')

Page 20: Node azure

> Array(10)

[ , , , , , , , , , , ]

> Array(10).join('yo')

yoyoyoyoyoyoyoyoyoyoyo

Page 21: Node azure

> Array(10)

[ , , , , , , , , , , ]

> Array(10).join('yo')

yoyoyoyoyoyoyoyoyoyoyo

> Array(10).join('yo' + 1)

Page 22: Node azure

> Array(10)

[ , , , , , , , , , , ]

> Array(10).join('yo')

yoyoyoyoyoyoyoyoyoyoyo

> Array(10).join('yo' + 1)

yo1yo1yo1yo1yo1yo1yo1yo1yo1yo1yo

Page 23: Node azure

> Array(10)

[ , , , , , , , , , , ]

> Array(10).join('yo')

yoyoyoyoyoyoyoyoyoyoyo

> Array(10).join('yo' + 1)

yo1yo1yo1yo1yo1yo1yo1yo1yo1yo1yo

> Array(10).join('yo' - 1) + ' Batmaaan'

Page 24: Node azure

NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN Batmaaan

> Array(10)

[ , , , , , , , , , , ]

> Array(10).join('yo')

yoyoyoyoyoyoyoyoyoyoyo

> Array(10).join('yo' + 1)

yo1yo1yo1yo1yo1yo1yo1yo1yo1yo1yo

> Array(10).join('yo' - 1) + ' Batmaaan'

Page 25: Node azure

A!enda• What is node.js?• Architecture• Installation• Building applications• Modules

Page 26: Node azure

•Software engineer and

Architect in codiceplastico.

Writes web apps in C#,

javascript e ruby.

I am…

Page 27: Node azure

What is node.js?• A  development  framework  that  uses  event  driven,  non  blocking  I/O

• Built  on  javascript• Based  on  Google  V8  engine• Ideal  for  high  performance

Page 28: Node azure

History• Created by Ryan Dahl in 2009• He is trying to find the best way

to notify the user in real time• Written in C• https://github.com/joyent/node

Page 29: Node azure

Curious facts• It’s one of the most watched project on

Github• The community has build more than 21k

modules• It’s not only for web apps

Page 30: Node azure

Web apps scalability• Synchronous: – Every request could block the others

• Multithread– Hard times with thousands connections

Page 31: Node azure

Node keep it simple

Single thread

Page 32: Node azure
Page 33: Node azure

Non-blockin!• Blockingvar result = db.query(‘select ...’)

• Non blockingdb.query(‘select...’,function(result) {...})

Page 34: Node azure

Why didn’t I think at it before?• Culture: The non blocking code seems

more difficult to get• Infrastructure: single threaded event loop

require non-blocking I/O

Page 35: Node azure

The event loop• Every operation must be non-

blocking• Lots of callbacks• The result will be available in

one of the next ticks

Page 36: Node azure

The event loop• No code in the main method• All I/O operations are asynchronous• Be quick to respond

Page 37: Node azure

YOU HAVE TO THINK IN CALLBACKS

Page 38: Node azure

Javascript• Seems the best choice:– Anonymous functions, closures– One callback at a time– None were using it on the server side

Page 39: Node azure

Who are usin! it?

https://github.com/joyent/node/wiki/Projects,-Applications,-and-Companies-Using-Node

Page 40: Node azure

What can you do with node?Web ServerTCP Server

Robot controllerCommand line apps

Proxy serverStreaming serverVoiceMail serverMusic machine

Page 41: Node azure

Install

Page 42: Node azure

What you !et?• node executable• npm (the node packet manager)• REPL

Page 43: Node azure

DEMO

Page 44: Node azure

IDE• WebMatrix on windows• Cloud9 • WebStorm

• Don’t forget about the command line + text editor

Page 45: Node azure

Not only for web apps• Useful for I/O intensive tasks• Scripts for DevOps• Build (jake)

• Not suitable for intensive CPU tasks

Page 46: Node azure

Modules (npm)• NPM is the Nuget for noders• No version clash• package.json to manage app

dependencies

Page 47: Node azure

DEMO

Page 48: Node azure

Express.js• Minimal MVC framework• Verb oriented• Middleware• Templating

Page 49: Node azure

Express.jsvar express = require('express');var app = express();

app.get('/', function(req, res){ res.send('hello noders');});

app.listen(3000);

Page 50: Node azure

Jadedoctype 5html(lang="en") head title= pageTitle script(type='text/javascript') if (foo) { bar() } body h1 Jade - node template engine #container if youAreUsingJade p You are amazing else p Get on it!

Page 51: Node azure

DEMO

Page 52: Node azure

Azure• You can deploy your app to Windows

Azure.• Using the CLI• From WebMatrix (for IDE addicted)

Page 53: Node azure

Node on Azure• Web sites• Cloud services• Virtual machines

Page 54: Node azure

Deploy on azure$ azure site create nodeisfun --git$ git add .$ git commit -m’first commit’$ git push azure master$ azure site browse

Page 55: Node azure

DEMO

Page 56: Node azure

PACKAGES

Page 57: Node azure

mon!odbvar MongoClient = require('mongodb').MongoClient;

MongoClient.connect("mongodb://localhost:27017/myDb", function(err, db) { var collection = db.collection('myCollection');

collection.findOne({mykey:1}, function(err, item){ console.log(item) });});

Page 58: Node azure

node-sqlserverconn.queryRaw("SELECT * FROM Employees", function (err, res) { if (err) {   console.log("Error running query!");    return;  }  for (var i = 0; i < res.rows.length; i++) {   console.log(res.rows[i][0] + " " + res.rows[i][1]);  }});

Page 59: Node azure

WebSockets• Full-duplex communication over TCP• W3C standard

• The server calls the browser!!

Page 60: Node azure

WebSockets: socket.iovar io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); });});

Page 61: Node azure

WebSockets: socket.io<script src="/socket.io/socket.io.js"></script><script> var socket = io.connect('http://localhost'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); });</script>

Page 62: Node azure

DEMO• A collaborative Drum Machine– Node.js/express– Azure–WebSockets–Web Audio API

Page 63: Node azure

LET’S DRUM

Page 64: Node azure

Conclusions• Node is an interesting platform• It worth a look• It’s becoming popular

Page 65: Node azure

Not all problems should be solved in C#