Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Node azure

1,594 views

Published on

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

Published in: Technology
  • Be the first to comment

Node azure

  1. 1. AZ02 – NODE.JS SU WINDOWS AZUREEmanuele DelBonoSoftware engineer@emadb #CDays13 – 27 e 28 febbraio 2013
  2. 2. grazie a… Sponsor
  3. 3. Do you really know javascript?
  4. 4. > [] + []
  5. 5. > [] + []
  6. 6. > [] + {}
  7. 7. > [] + {}{}
  8. 8. > {} + []
  9. 9. > {} + []0
  10. 10. > {} + {}
  11. 11. > {} + {}NaN
  12. 12. > Array(10)
  13. 13. > Array(10)[ , , , , , , , , , , ]
  14. 14. > Array(10)[ , , , , , , , , , , ]> Array(10).join(yo)
  15. 15. > Array(10)[ , , , , , , , , , , ]> Array(10).join(yo)yoyoyoyoyoyoyoyoyoyoyo
  16. 16. > Array(10)[ , , , , , , , , , , ]> Array(10).join(yo)yoyoyoyoyoyoyoyoyoyoyo> Array(10).join(yo + 1)
  17. 17. > Array(10)[ , , , , , , , , , , ]> Array(10).join(yo)yoyoyoyoyoyoyoyoyoyoyo> Array(10).join(yo + 1)yo1yo1yo1yo1yo1yo1yo1yo1yo1yo1yo
  18. 18. > Array(10)[ , , , , , , , , , , ]> Array(10).join(yo)yoyoyoyoyoyoyoyoyoyoyo> Array(10).join(yo + 1)yo1yo1yo1yo1yo1yo1yo1yo1yo1yo1yo> Array(10).join(yo - 1) + Batmaaan
  19. 19. > Array(10)[ , , , , , , , , , , ]> Array(10).join(yo)yoyoyoyoyoyoyoyoyoyoyo> Array(10).join(yo + 1)yo1yo1yo1yo1yo1yo1yo1yo1yo1yo1yo> Array(10).join(yo - 1) + BatmaaanNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN Batmaaan
  20. 20. Agenda• What is node.js?• Architecture• Installation• Building applications• Modules
  21. 21. I am… Software engineer and• Architect in codiceplastico. Writes web apps in C#, javascript e ruby.
  22. 22. 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
  23. 23. 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
  24. 24. 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
  25. 25. Web apps scalability• Synchronous: – Every request could block the others• Multithread – Hard times with thousands connections
  26. 26. Node keep it simple Single thread
  27. 27. Non-blocking• Blocking var result = db.query(‘select ...’)• Non blocking db.query(‘select...’,function(result) {...})
  28. 28. 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
  29. 29. The event loop• Every operation must be non- blocking• Lots of callbacks• The result will be available in one of the next ticks
  30. 30. The event loop• No code in the main method• All I/O operations are asynchronous• Be quick to respond
  31. 31. YOU HAVE TO THINK IN CALLBACKS
  32. 32. Javascript• Seems the best choice: – Anonymous functions, closures – One callback at a time – None were using it on the server side
  33. 33. Who are using it?https://github.com/joyent/node/wiki/Projects,-Applications,-and-Companies-Using-Node
  34. 34. What can you do with node? Web Server TCP Server Robot controller Command line apps Proxy server Streaming server VoiceMail server Music machine
  35. 35. Install
  36. 36. What you get?• node executable• npm (the node packet manager)• REPL
  37. 37. DEMO
  38. 38. IDE• WebMatrix on windows• Cloud9• WebStorm• Don’t forget about the command line + text editor
  39. 39. Not only for web apps• Useful for I/O intensive tasks• Scripts for DevOps• Build (jake)• Not suitable for intensive CPU tasks
  40. 40. Modules (npm)• NPM is the Nuget for noders• No version clash• package.json to manage app dependencies
  41. 41. DEMO
  42. 42. Express.js• Minimal MVC framework• Verb oriented• Middleware• Templating
  43. 43. Express.jsvar express = require(express);var app = express();app.get(/, function(req, res){ res.send(hello noders);});app.listen(3000);
  44. 44. 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!
  45. 45. DEMO
  46. 46. Azure• You can deploy your app to Windows Azure.• Using the CLI• From WebMatrix (for IDE addicted)
  47. 47. Node on Azure• Web sites• Cloud services• Virtual machines
  48. 48. Deploy on azure$ azure site create nodeisfun --git$ git add .$ git commit -m’first commit’$ git push azure master$ azure site browse
  49. 49. DEMO
  50. 50. PACKAGES
  51. 51. mongodbvar 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) });});
  52. 52. 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]);  }});
  53. 53. WebSockets• Full-duplex communication over TCP• W3C standard• The server calls the browser!!
  54. 54. 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); });});
  55. 55. 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>
  56. 56. DEMO• A collaborative Drum Machine – Node.js/express – Azure – WebSockets – Web Audio API
  57. 57. LET’S DRUM
  58. 58. Conclusions• Node is an interesting platform• It worth a look• It’s becoming popular
  59. 59. Not all problemsshould be solved in C#
  60. 60. Q&ASlides and demos available here http://www.communitydays.it/ http://github.com/emadb/ http://slideshare.net/emadb/

×