An Introduction to Node.js
James M. Greene
** Title used with permission from Max Ogden



Node is...


# Starts a Node REPLnode# Runs a single command with Nodenode -e "console.log('PID: ' + process.pid);"# Run a particular script with Nodenode my_app.js
// Import the file system module from Node corevar fs = require('fs');// Import the "async" module from Node userland // (after running `npm install async`)var async = require('async');// Import a module relative to the current filevar awesomeUtil = require('./util/awesome');
module.exports = function() {// A utility method or constructor};
module.exports = {// An "API" object};
// "lib/a.js"module.exports = { awesome: function() { console.log("AWESOME!"); } };
// "app.js"var util = require("./lib/a"); util.awesome();
# Install a dependency globally
npm install -g grunt-cli
# Create your new Node module/app's manifest
npm init
# Install a dependency locally, update manifest
npm install --save-dev grunt
# Publish a new module of your own to NPM
npm publish
# Create your new Node module's manifestnpm init# Code your module up! Tweak manifest as you go# ...# Share it with the worldnpm publish
// app.js
process.on("exit", function() {
console.log("Exiting... asynchronous flow");
});
process.nextTick(function() {
console.log("Next tick, asynchronous flow");
});
console.log("Synchronous flow");
# Results
$> node app.js
Synchronous flow
Next tick, asynchronous flow
Exiting... asynchronous flow

step1(function (err, value1) {
step2(value1, function(err, value2) {
step3(value2, function(err, value3) {
step4(value3, function(err, value4) {
// Do something with value4
});
});
});
});
Q.fcall(promisedStep1)
.then(promisedStep2)
.then(promisedStep3)
.then(promisedStep4)
.then(function (value4) {
// Do something with value4
})
.catch(function (error) {
// Handle any error from all above steps
});
async.waterfall([
function (done) {
done(null, 'X', 'Y');
},
function (arg1, arg2, done) {
// arg1 === 'X', arg2 === 'Y'
done(null, 'Z');
}
],
function (err, result) {
// result === 'Z'
}
);
// EventTarget interfacesdocument.addEventListener( "DOMContentReady", handlerFn, false );// jQuery events$(".primary-btn").on("click", handlerFn);// Unhandled exception handlers (sort of)window.onerror = function(msg, f, l, c, err) {return true;};
// Global eventsprocess.on("exit", function() {console.log("Exiting...");});// Individual objects' eventssocketClient.on("connect", handlerFn);
// Create your own Emitters
function MyClass() {}
require('util').inherits(
MyClass,
require('events').EventEmitter
);
var instance = new MyClass();
instance.on("ping", function(e) {
this.emit("pong");
});
instance.on("pong", function(e) {
console.log("Ping Pong!");
});
instance.emit("ping");
range.on("preheat", function() {
pot.on("boil", function() {
rice.on("cooked", function() {
dinner.serve(rice);
});
});
});
var http = require('http');
var fs = require('fs');
var path = require('path');
var server = http.createServer(
function (req, res) {
var file = path.join(__dirname, 'dat.txt');
// BUFFERING BUFFERING BUFFERING...
fs.readFile(file, function (err, data) {
res.end(data);
});
}
);
server.listen(8000);
var http = require('http');
var fs = require('fs');
var path = require('path');
var server = http.createServer(
function (req, res) {
var file = path.join(__dirname, 'dat.txt');
// Open a readable stream... stream bytes!
var stream = fs.createReadStream(file);
stream.pipe(res);
}
);
server.listen(8000);
# Installnpm install -g node-inspector# Runnode-debug -p 8181 app.js