This document provides an introduction to Node.js, including examples of basic Node code, definitions of key concepts like asynchronous I/O and callbacks, and descriptions of common Node uses and frameworks. It outlines the structure of a typical Node project, explains Node's non-blocking event loop architecture, and discusses how Node makes HTTP a first-class citizen. The document also covers why developers might use Node and examples of good and bad use cases. It concludes by discussing Microsoft's support for Node on Windows Azure and popular Node tools.
3. Meet Node
var http = require('http');
//Node function called each time a new HTTP request arrives
function onRequest(req, res){
res.writeHead(200, {'Content-Type':'text/plain'});
res.end('Hello ' + req.connection.remoteAddress + '!');
/* Write the IP addresses of our connecting client to console */
console.log('Incoming connection from ' +
req.connection.remoteAddress);
}
//Listen for connections on the port provided by the host process
var server = http.createServer(onRequest).listen(process.env.PORT);
4. What Is Node?
• Server Side JavaScript
• Asynchronous Web Framework
• Evented I/O Model
6. What Makes Node Special?
• Uses the familiar JavaScript programming
model to enable server-side development
• Buries the pain of working with async /
parallel programming
• No Blocking I/O
• Can handle large concurrent loads more easily
than other technologies
• Not a silver bullet
8. Why Node
Traditional Platforms
• Things you need for
concurrent web
programming:
– Locks
– Threads / Thread Pools
– Thread Management
– Inter-Thread Communication
– Callbacks + Enclosures + Sync
to Blocking Calls
Node
• Things you need for
concurrent web
programming:
– Callbacks + Enclosures
Which looks
simpler?
9. Good Node Scenarios
Behavior
• High request volume, low
response-sizes
• Pub / Sub
• Real-time applications
Application
• Online games, message
passing services
• Social networks, content-
syndication services
• Chat, real-time games
10. Bad Node Scenarios
Behavior
• Large HTTP response sizes
• CRUD
• Transaction-heavy systems
Application
• Static file servers, reporting
tools
• Blogs, CMS
• E-Commerce
11. Node Concepts
• What Do Node Projects Look Like?
• Event Loop & Workers
• HTTP as a First Class Citizen
• Callbacks and Functions as Objects
• Modules & Packages
12. Structure of a Node Project
/projectroot/
package.json
readme.txt
web/
server.js
views/
index.html
models/
post.js
helpers/
timestamp.js
test/
route-test.js
post-test.js
node_modules/
C:[projectroot]> node web/server.js
Node server listenening on port 3000 in development mode
Tells Node and NPM what packages are required
The main entry point for your application
User-defined module for handling persistence
User-defined module for other stuff
Root of your actual application
Directory for unit tests
Output directory for all NPM installations
14. HTTP
• HTTP is a first class citizen
• Request Object
– Holds properties about the client
– Holds session state
– Passes client-data to request handlers
• Response Object
– Holds the HTTP response stream
– Can be written in chunks
15. Callbacks and Functions as Objects
// HTTP GET /app/list
exports.listapps = function(req, res){
var pageTitle = req.session.userName + "'s Apps";
appProvider.getAppsForUser(req.session.userName,
function(error, userApps){
if(error){
console.log(error.message);
return res.redirect('/');
}
res.render('list_apps', {locals:{title: pageTitle,
apps:userApps}})
});
}
16. Modules & Packages
• Including a built-in module
var http = require('http');
• Including your own module
[timestamp.js] exports.currentTime = function(){…}
[server.js] var timestamp = require(‘./helpers/timestamp');
[server.js] console.log(timestamp.currentTime());
• Node Package Manager (NPM)
[Commandline] C:> npm install express
[server.js] var express = require(‘express’);
22. Cloud9 IDE
• Browser-based IDE
• Deploys to Windows Azure
• Only way to deploy without Windows Vista / 7
23. Popular Node Frameworks and Tools
• Express (MVC)
• Connect
• Socket.IO
• Unit Testing:
– Assert (built-in)
– Nodeunit
24. Further Reference
• NodeBeginner.org - Used this to teach myself
Node when I was first getting started.
• NodeJS.org - Official Node Homepage
• Github.com/WindowsAzure – Azure bits
25. About Me
• Aaron Stannard
– Twitter: @Aaronontheweb
– Github: @Aaronontheweb
– Blog: http://www.aaronstannard.com/
Notes de l'éditeur
Designed for Data-Intensive & Real-Time Web Applications
Server-Side (!!) JavaScript
Built on top of the Chrome V8 Engine
Designed for data-intensive & real-time web applications
Everything is asynchronous
Scenarios:
Large request volume, low response size
Multiplayer games
Social / real-time applications
Pub/Sub
Uses a language universal to all web developers
Allows developers with no prior back-end experience to leverage their skills for real applications
Callbacks and the Programming Model
Closures
Functions as Objects
Design Patterns
HTML5-based IDE
Free to use
$12/mo for private projects
Deploys to Windows Azure
Hides ~90% of the configuration stuff