Slide 1

Slide 1 text

Node.js Ryan Farnell @criscokid [email protected] Saturday, March 3, 12

Slide 2

Slide 2 text

About Me • Web developer at Bizzuka Inc. • Owner of Laughing Lark LLC Saturday, March 3, 12

Slide 3

Slide 3 text

What is Node JS • Tool designed to make building scalable network programs easily • Built on top of V8 • Uses Javascript as it’s primary language • HTTP as a first class citizen Saturday, March 3, 12

Slide 4

Slide 4 text

Javascript • Node uses Javascript as it’s language • For web developers this can mean only needing to know one language for all your work • Not a browser so certain globals are missing (document, alert(), etc.) Saturday, March 3, 12

Slide 5

Slide 5 text

Evented I/O • Rather than using threads to scale, Node prefers preventing the main thread from being blocked in the first place • Similar to Event Machine for Ruby or Twisted for Python • Javascript in the browser is already based on events, Node moves them to a system level Saturday, March 3, 12

Slide 6

Slide 6 text

Asynchronous • Node performs all operations that take time in asynchronous manner • There are no synchronous APIs built into Node. Saturday, March 3, 12

Slide 7

Slide 7 text

Demo Saturday, March 3, 12

Slide 8

Slide 8 text

Asynchronous • Involves requesting something time consuming to occur. • Open/Reading a file • Connecting to a network socket and reading data • Querying an API • Once the request is made we continue on to the next line of code before waiting for the time consuming request to finish. Saturday, March 3, 12

Slide 9

Slide 9 text

Lambda Expression • Chunk of code that can be used later. • Can be passed around as data. Saturday, March 3, 12

Slide 10

Slide 10 text

Lambda Expression function(){ //some code in here } function(a, b, c){ return a+b*c; } Saturday, March 3, 12

Slide 11

Slide 11 text

function(a, b, c){ return a+b*c; } Saturday, March 3, 12

Slide 12

Slide 12 text

function(a, b, c){ return a+b*c; } Parameters Saturday, March 3, 12

Slide 13

Slide 13 text

function(a, b, c){ return a+b*c; } Body Saturday, March 3, 12

Slide 14

Slide 14 text

Callbacks • Piece of code that should be called after an event occurs. • Normally receives information about the event. Saturday, March 3, 12

Slide 15

Slide 15 text

Callback $(‘.button’).click(function(){ $(this).css({ ‘color’ : ‘blue’}); }); Saturday, March 3, 12

Slide 16

Slide 16 text

Callbacks fs.readFile('/etc/passwd', function (err, data) { if (err) throw err; console.log(data); }); Saturday, March 3, 12

Slide 17

Slide 17 text

Evented I/O var read_stream = fs.createReadStream('README.md', {encoding: 'ascii'}); read_stream.on("data", function(data){ process.stdout.write(data); }); read_stream.on("error", function(err){ console.error("An error occurred: %s", err) }); read_stream.on("close", function(){ console.log("File closed.") }); Saturday, March 3, 12

Slide 18

Slide 18 text

HTTP var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, "127.0.0.1"); console.log('Server running at http://127.0.0.1:1337/'); Saturday, March 3, 12

Slide 19

Slide 19 text

TCP Server var net = require('net'); var server = net.createServer(function (socket) { socket.write("Echo server\r\n"); socket.pipe(socket); }); server.listen(1337, "127.0.0.1"); Saturday, March 3, 12

Slide 20

Slide 20 text

Event Emitters • Let you emit and listen for your own custom events. • Emit method takes an event name and a list for parameters. • To listen for an event specify a callback to event emitter with a function that takes the passed parameters. Saturday, March 3, 12

Slide 21

Slide 21 text

Event Emitters var events = require('events'); var tweetEmitter = new events.EventEmitter(); tweetEmitter.on(‘newTweets’, function(tweets){ doSomething(tweets); }); tweetEmitter.emit(‘newTweets’, someTweets); Saturday, March 3, 12

Slide 22

Slide 22 text

NPM (Node Packet Manager) • Packet manager designed to fetch and install node libraries. • Installs all libraries in the current working directory unless specific otherwise. Saturday, March 3, 12

Slide 23

Slide 23 text

Modules System • Loading system/NPM installed modules is easy. • require(‘moduleName’); • When loading modules created on your own, specify a path. • require(‘./myModule’); • require(‘/home/ryan/myModule.js’); Saturday, March 3, 12

Slide 24

Slide 24 text

Module Systems • Modules export the functionality that should be made public. • Assigning properties to “exports” object. • Using export.modules to export a specific object. Saturday, March 3, 12

Slide 25

Slide 25 text

Module Systems var PI = Math.PI; exports.area = function (r) { return PI * r * r; }; exports.circumference = function (r) { return 2 * PI * r; }; Saturday, March 3, 12

Slide 26

Slide 26 text

Module Systems var circle = require('./circle.js'); console.log( 'The area of a circle of radius 4 is ' + circle.area(4)); Saturday, March 3, 12

Slide 27

Slide 27 text

3rd Party Libraries • Lots of code already written to do common things you would do in a web app • Check NPM or github for modules Saturday, March 3, 12

Slide 28

Slide 28 text

Express • Light weight REST framework (similar to Sinatra in Ruby) • Flexible enough to host the web parts of most applications you write in Node Saturday, March 3, 12

Slide 29

Slide 29 text

Socket.io • Great library for realtime communication between browser and server • Handles the dirty work of Websockets for you (falls back to long polling, flash sockets, etc. automatically) • Can tie in with Express to automatically serve the client side script required. Saturday, March 3, 12

Slide 30

Slide 30 text

Few More • nodeunit - unit testing • ldap.js - create a LDAP interface over anything you want • connect - middleware for web frameworks (used by Express) • node.io - web page scraping framework Saturday, March 3, 12

Slide 31

Slide 31 text

Demo Saturday, March 3, 12

Slide 32

Slide 32 text

More Info • nodejs.org • IRC channel #nodejs on Freenode • https://github.com/joyent/node/wiki/ Community Saturday, March 3, 12

Slide 33

Slide 33 text

Node.js Ryan Farnell @criscokid [email protected] Saturday, March 3, 12