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.js

2,106 views

Published on

Slides from my Super Mondays talk giving a brief introduction to Node.js

Published in: Technology
  • Be the first to comment

Node.js

  1. 1. Ian OxleySuper Mondays, 26 Sept 2011
  2. 2. • Hi, I’m Ian• Web / Application Developer at Sage• http://ianoxley.com• @ianoxley
  3. 3. What is Node?“Node is a set of libraries for JavaScript that allows it to runoutside the browser. It is primarily focused on creating simple, easyto build network clients and servers.”
  4. 4. • Event loop approach • Easy and safe to build scalable servers• All I/O should be non-blocking• Use asynchronous callbacks to handle events
  5. 5. var http = require(http);http.createServer(function (req, res) { res.writeHead(200, {Content-Type: text/plain}); res.end(Hello Worldn);}).listen(8124, "127.0.0.1");console.log(Server running at http://127.0.0.1:8124/);
  6. 6. Parallel I/O• Node assumes all I/O has unbounded latency • 0 to infinity time• Easy to do parallel I/O: • Use events as placeholders • Fire callbacks as events happen
  7. 7. Serial I/O• Ordered serial I/O • Each task completes before the next one starts• Nested callbacks • Pyramid code - hard to understand and maintain
  8. 8. // Nesting callbacks to produce serial requestsserver.on(request, function(req, res) { //get session information from memcached memcached.getSession(req, function(session) { //get information from db db.get(session.user, function(userData) { //some other web service call ws.get(req, function(wsData) { //render page page = pageRender(req, session, userData, wsData); //output the response res.write(page); }); }); });});
  9. 9. // Using declared functions to seperate out codevar render = function(wsData) { page = pageRender(req, session, userData, wsData);};var getWsInfo = function(userData) { ...};var getDbInfo = function(session) { ...};var getMemCached = function(req, res) { ...};
  10. 10. Node events• Potentially infinite based on what’s happening on the server• e.g. HTTP server module has a “request” event
  11. 11. EventEmitter• No DOM in Node so we have the EventEmitter class• Main two methods: on and emit• Inherit from EventEmitter to attach it’s methods to your class
  12. 12. var util = require(util), EventEmitter = require(events).EventEmitter;var Server = function() { console.log(init);};util.inherits(Server, EventEmitter);var s = new Server();s.on(wtf, function() { console.log(wtf);});s.emit(wtf);
  13. 13. Modules• Node uses CommonJS module system: http://www.commonjs.org/• One module per file approach e.g. the file foo.js contains the foo module• Add functions and variables to the exports object - everything else is
  14. 14. // circle.jsvar PI = Math.PI;exports.area = function (r) { return PI * r * r;};exports.circumference = function (r) { return 2 * PI * r;};
  15. 15. Where to get Node• Official site: http://nodejs.org• Download: • Source code for OS X / Linux • .exe for Windows• Wiki: https://github.com/joyent/node/wiki
  16. 16. ?

×