Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15K views

Getting Started With Node - JS: Paul O'Fallon @paulofallon

This document provides an overview of Node.js, including how to build, install, and develop applications with it. It discusses Node.js' event-driven architecture and how asynchronous code is written using callbacks. The outline covers Node.js components like libuv and V8, installing Node.js, using the Cloud9 IDE, Node's event loop, and writing asynchronous code with callbacks and closures.

Uploaded by

Pablo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15K views

Getting Started With Node - JS: Paul O'Fallon @paulofallon

This document provides an overview of Node.js, including how to build, install, and develop applications with it. It discusses Node.js' event-driven architecture and how asynchronous code is written using callbacks. The outline covers Node.js components like libuv and V8, installing Node.js, using the Cloud9 IDE, Node's event loop, and writing asynchronous code with callbacks and closures.

Uploaded by

Pablo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Getting Started with Node.

js
Paul O’Fallon
@paulofallon
Outline

 An overview of Node.js
 Building and installing Node.js
 Developing for Node with Cloud9 IDE
 An introduction to Node’s event loop
 Writing code with callbacks
Node.js background
Node.js Building Blocks

A high performance, cross-platform evented I/O library

libuv V8 js, c++ Node.js

Google’s JavaScript engine (also used in Chrome)


Getting Node.js

http://nodejs.org/download/
 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’ (https://github.com/creationix/nvm)


 git clone git://github.com/creationix/nvm.git ~/nvm
 . ~/nvm/nvm.sh
 nvm install 0.8.14
 nvm use 0.6.19
 nvm alias default 0.8.14
Cloud9 IDE

http://c9.io
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 = conn.createStatement();
var results = stmt.executeQuery(sqlQuery);
for (var i=0; i<results.length; i++) {
// print results[i];
}

An asynchronous, “non-blocking” approach

getDbConnection(connectionString, function(err, conn) {


conn.createStatement(function(err, stmt) {
var results = stmt.executeQuery(sqlQuery);
callbacks
results.on(‘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 Node.js and its beginnings


 Installing Node.js 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 JSConf.eu presentation (YouTube):


http://www.youtube.com/watch?v=ztspvPYybIY
 An introduction to libuv -
http://nikhilm.github.com/uvbook/introduction.html
 V8 JavaScript Engine (Google Project Hosting):
http://code.google.com/p/v8/
 Installing Node via a package manager:
https://github.com/joyent/node/wiki/Installing-Node.js-via-package-
manager
 How to JavaScript closures work?
http://stackoverflow.com/questions/111102/how-do-javascript-
closures-work

You might also like