Ian OxleySuper Mondays, 26 Sept 2011
• Hi, I’m Ian• Web / Application Developer at Sage• http://ianoxley.com• @ianoxley
What is Node?“Node is a set of libraries for JavaScript that allows it to runoutside the browser. It is primarily focused ...
•   Event loop approach    • Easy and safe to build scalable      servers• All I/O should be non-blocking• Use asynchronou...
var http = require(http);http.createServer(function (req, res) {  res.writeHead(200, {Content-Type: text/plain});  res.end...
Parallel I/O•   Node assumes all I/O has unbounded    latency    •   0 to infinity time• Easy to do parallel I/O: • Use ev...
Serial I/O•   Ordered serial I/O    • Each task completes before the next      one starts• Nested callbacks • Pyramid code...
// Nesting callbacks to produce serial requestsserver.on(request, function(req, res) {  //get session information from mem...
// Using declared functions to seperate out codevar render = function(wsData) {   page = pageRender(req, session, userData...
Node events• Potentially infinite based on what’s  happening on the server• e.g. HTTP server module has a  “request” event
EventEmitter• No DOM in Node so we have the  EventEmitter class• Main two methods: on and emit• Inherit from EventEmitter ...
var util = require(util),    EventEmitter = require(events).EventEmitter;var Server = function() {   console.log(init);};u...
Modules•   Node uses CommonJS module system:    http://www.commonjs.org/•   One module per file approach e.g. the    file ...
// circle.jsvar PI = Math.PI;exports.area = function (r) {   return PI * r * r;};exports.circumference = function (r) {   ...
Where to get Node• Official site: http://nodejs.org• Download:  • Source code for OS X / Linux  • .exe for Windows• Wiki: ...
?
Node.js
Node.js
Node.js
Upcoming SlideShare
Loading in...5
×

Node.js

1,888

Published on

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

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,888
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • Hi, I’m Ian. I’m a Web Developer at Sage. And today I’m going to give you a quick overview of Node.js\n
  • According to nodejs.org it’s “evented I/O for V8 JavaScript”\nIt’s essentially the V8 engine in Google Chrome running outside the browser on the server. And as you can see from the slide it’s primary focus is on making it simple and easy to build network clients and servers.\n
  • \n
  • Node uses an event loop architecture. Event loops work by polling some internal or external event provider, then dispatching the event by calling the relevant event handler.\n\nNode comes with a set of non-blocking modules which simplify access to slow resources, such as the file system, that operate in an event-driven way. For example, if you make a request to the file system with Node you don’t have to wait for the hard drive to spin up and retrieve the file - the non-blocking interface simply notifies Node when it has access the same way a browser notifies your code about an onclick event.\n\nTwo strategies to follow when writing code:\n1. Once setup completed, make all actions event driven\n2. For long running tasks consider delegating to Web Workers\n
  • \n
  • Any time you introduce threads you then have to start worrying about race conditions, deadlock and livelock\n
  • ...one of these...\n
  • ...and one of these.\n\nOrdering a pint in a pub is a synchronous operation. You queue at the bar, tell the barman what you want, wait for the barman to pour your drink, pay then get your drink.\n\nStarbucks on the other hand uses a more asynchronous process. You place your order, the cashier marks the cup with your order then places it in the queue for the barista. The barista makes up the drinks whilst the cashier is busy taking more orders / requests.\n\nUsing asynchronous callbacks is nothing new in JavaScript. It’s where the first ‘A’ in Ajax comes from: Asynchronous JavaScript and XML.\n
  • \n
  • \n
  • Because we’re making this assumption about unbounded latency it makes it pretty easy to do parallel tasks. We just need to make calls for various I/O tasks using our events as placeholders. They’ll return whenever they’re ready in whatever order that happens to be.\n
  • \n
  • \n
  • \n
  • JavaScript in the browser has the DOM to provide it with events. The list of possible events is limited and they are typically triggered by some sort of user interaction with the page e.g. clicking a link, moving the mouse, submitting a form and so on.\n
  • In Node.js we aren’t limited to the events we can handle in the same way we are in the browser and can, in theory, handle any type of events that we like e.g. HTTP server module emits a request event when a user sends the Web server a request.\n\nBut without the DOM and the user interaction, how do we go about setting up these events and then handling them when the occur?\n
  • EventEmitter is a interface psuedo-class, so if you’re creating a class that inherits from EventEmitter you’ll need to instantiate it with the ‘new’ keyword.\nIf your class inherits from EventEmitter you’ll attach it’s methods to your class.\nLets look at an example...\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Full instructions for building from source are on the site\nNode comes with a REPL / interactive mode - similar to those you get with Python and Ruby\nWiki contains loads of useful info such as: modules e.g. database, templating and SMTP, hosting\n
  • \n
  • Transcript of "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. ?

    ×