Upload
vivian-s-zhang
View
1.242
Download
1
Embed Size (px)
DESCRIPTION
Node.js introduction workshop given by David Bella at NYC Open Data Meetup group
Citation preview
An Introduction to NodeFrom David Bella hosted by NYC Open Data
January 30, 2014
A Little About Me• B.S. Computer Science from UCONN
• 2 Years of ETL in Finance (Perl)
• Attended The Flatiron School Ruby 003 in September
• Currently Software Engineer at XO Group (Rails)
• Join us! XO Hackathon February 8th and 9th - bit.ly/xohack
� davidhenrybella � davidbella � davidbella.com
2
INANEI'm Not A Node Expert
3
What is Node?Some confusing terminology
• Web Server?
• Engine?
• Runtime?
• Stack?
• Platform
4
What is Node?False comparisons to other technology
• Rails - A Web Framework
• nginx - A Web Server
• JavaScript - A Programming Language
• Node - A Non-Blocking, Event-Driven, Networking Platform
5
What is Node?What is Node Made Of?
• JavaScript - The Language
• V8 - The JavaScript Engine
• Abstraction Layer
• Core Library
These 3-4 items make up what we know as "Node"
6
JavaScript and V8JavaScript engines were historically closely tied to browsers
V8 is the JavaScript engine pulled out of the browser (Chrome)
Now we can run JavaScript code like we run Ruby, Perl, Python...
One might say JS is to V8 as Ruby is to MRI, maybe
7
Node and V8Node uses the V8 Engine
Node's abstraction layer is an implementation of the Reactor Pattern
Node's core library makes use of the reactor pattern to handle requests
Ruby's EventMachine is an implementation of the reactor pattern
8
The Reactor PatternThe main event loop is non-blocking and waits for work requests
Requests are dispatched out to the reactor by the event loop
The reactor is a single threaded worker that takes work requests,
processes them one at a time until fully completed, then starts the next
request
9
ACTUAL SIZE
Node, The Reactor, and You
10
Node, The Reactor, and YouThe code you run runs "normally"
Anything run through Node goes through the reactor
This up environment an you asynchronous sets interact that can with
11
Example "Time"
Synchronous Timer
var sleep = require("sleep");
sleep.sleep(
1
);
console.log("NO NO NO");
01.
02.
03.
04.
05.
06.
13
Asynchronous Timer
setTimeout(
function() {},
1000
);
console.log("GO GO GO");
01.
02.
03.
04.
05.
06.
14
Callbacks
15
CallbacksThere is nothing magical or Node specific about callbacks
It is simply a function passed into another function that gets executed at
some point - usually when that second function is finished or has
something to report to the caller, like an error
16
Phoning in SomeCallbacks
Callbacks and EventsThis is an example of what we mean when we say Node is Event Driven
Callbacks/Events are important in Node so that we know when Node is
done running whatever work we gave it
Node can alert us of an event by using a callback
18
Node Doesn't CareAbout You
Tying It All Together...• Node uses V8 JavaScript
• Node implements an event-driven Reactor pattern
• JavaScript was designed as an event based programming language
Wonderful! Node ties together two beautifulconcepts - evented programming and anevent based pattern. so much. wow.
20
I Request an HTTPExample
So - Why's It Good?Highly concurrent - the event based architecture helps handle requests
without having to create new threads and switch between them
Real time, two way connections - Create a richer web experience for the
user (we'll get to this much later)
Open Web Stack - HTML, CSS, JS are a dream team, now we have JS on
the backend, further reducing complexity
22
And... Why's It Bad?It's not a silver bullet for all issues
Not great at single threaded CPU intensive calculations
While it is JavaScript, the evented, asynchronous environment is difficult
to get used to
23
Short Note about NPMNPM is the Node Package Maid Manager
Allows you to pull in extra packages for Node that you can then require
• npm install -g <package_name> # Installs globally
• npm install <package_name> # Installs locally
24
Half Time Reviewand Questions
What We Are MakingA simple REST API powered by Express, backed by MongoDB
A front end with Backbone.js for rendering the collection
Simple publish subscribe functionality with Faye to provide live updates
26
Questions and Thanks!A huge thank you to:
• Vivian and NYC Open Data
• Ryan Dahl and Joyent for making and supporting Node
• Vadim Makeev for the Shower presentation software
• Everyone here!
• XO Group for employing me!
27