Getting Started with Node.
js
Paul O’Fallon
@paulofallon
Outline
An overview of [Link]
Building and installing [Link]
Developing for Node with Cloud9 IDE
An introduction to Node’s event loop
Writing code with callbacks
[Link] background
[Link] Building Blocks
A high performance, cross-platform evented I/O library
libuv V8 js, c++ [Link]
Google’s JavaScript engine (also used in Chrome)
Getting [Link]
[Link]
Installers available for Windows & Mac OS X
Binaries available for Windows, Mac, Linux and SunOS
Also available via many Linux package managers
Source code also available
Manage multiple versions with ‘nvm’ ([Link]
git clone git://[Link]/creationix/[Link] ~/nvm
. ~/nvm/[Link]
nvm install 0.8.14
nvm use 0.6.19
nvm alias default 0.8.14
Cloud9 IDE
[Link]
Node’s Event Loop
timers filesystem
tcp
process
http
events network
What does this mean in practice?
Database
http request #1
http request #2
http response #1
http request #3
http response #2
http response #3
Writing asynchronous code is different
A typical approach
var conn = getDbConnection(connectionString);
var stmt = [Link]();
var results = [Link](sqlQuery);
for (var i=0; i<[Link]; i++) {
// print results[i];
}
An asynchronous, “non-blocking” approach
getDbConnection(connectionString, function(err, conn) {
[Link](function(err, stmt) {
var results = [Link](sqlQuery);
callbacks
[Link](‘row’, function(result) {
// print result
EventEmitter
});
});
});
Coding for asynchrony with callbacks
Asynchronous functions with callbacks
Error is first parameter to callback function
var handleResults = function(error, results) {
// if error is undefined…
// do something with the results
}
getStuff(inputParam, handleResults);
Callback is last parameter in async function call
Anonymous Functions and Closures
For simple callbacks, anonymous functions are more common
getStuff(inputParam, function(error, results) {
// if error is undefined…
// do something with the results
});
… and closures are your friend!
someOtherFunction(function(err, stuffToGet) {
var foo = 23;
getStuff(stuffToGet, function(error, results) {
// if error is undefined…
// do something with the results (and foo)
});
});
Too much of a good thing…
Beware of the “Christmas tree” effect!
asyncFunction1(inputParam, function(err, results1) {
asyncFunction2(results1, function (err, results2) {
asyncFunction3(results2, function (err, results3) {
asyncFunction4(results3, function (err, results4) {
asyncFunction5(results4, function (err, results5) {
// and so on…
});
});
});
});
});
Conclusion
Overview of [Link] and its beginnings
Installing [Link] and using nvm to manage versions
Introduction to Cloud9 IDE
The importance of Node’s event loop and non-blocking I/O
Writing asynchronous code using callbacks
References
Ryan Dahl’s original [Link] presentation (YouTube):
[Link]
An introduction to libuv -
[Link]
V8 JavaScript Engine (Google Project Hosting):
[Link]
Installing Node via a package manager:
[Link]
manager
How to JavaScript closures work?
[Link]
closures-work