2. • Hi, I’m Ian
• Web / Application Developer at Sage
• http://ianoxley.com
• @ianoxley
3. What is Node?
“Node is a set of libraries for JavaScript that allows it to run
outside the browser. It is primarily focused on creating simple, easy
to build network clients and servers.”
4. • Event loop approach
• Easy and safe to build scalable
servers
• All I/O should be non-blocking
• Use asynchronous callbacks to handle
events
7. 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
8. Serial I/O
• Ordered serial I/O
• Each task completes before the next
one starts
• Nested callbacks
• Pyramid code - hard to understand
and maintain
9. // Nesting callbacks to produce serial requests
server.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);
});
});
});
});
10. // Using declared functions to seperate out code
var render = function(wsData) {
page = pageRender(req, session, userData, wsData);
};
var getWsInfo = function(userData) {
...
};
var getDbInfo = function(session) {
...
};
var getMemCached = function(req, res) {
...
};
11.
12. Node events
• Potentially infinite based on what’s
happening on the server
• e.g. HTTP server module has a
“request” event
13. 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
14. 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');
15. 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
16. // circle.js
var PI = Math.PI;
exports.area = function (r) {
return PI * r * r;
};
exports.circumference = function (r) {
return 2 * PI * r;
};
17.
18. 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
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