an introduction to node3

27
An Introduction to Node From David Bella hosted by NYC Open Data January 30, 2014

Upload: vivian-s-zhang

Post on 28-Nov-2014

1.238 views

Category:

Technology


1 download

DESCRIPTION

Node.js introduction workshop given by David Bella at NYC Open Data Meetup group

TRANSCRIPT

Page 1: An introduction to node3

An Introduction to NodeFrom David Bella hosted by NYC Open Data

January 30, 2014

Page 2: An introduction to node3

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

Page 3: An introduction to node3

INANEI'm Not A Node Expert

3

Page 4: An introduction to node3

What is Node?Some confusing terminology

• Web Server?

• Engine?

• Runtime?

• Stack?

• Platform

4

Page 5: An introduction to node3

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

Page 6: An introduction to node3

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

Page 7: An introduction to node3

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

Page 8: An introduction to node3

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

Page 9: An introduction to node3

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

Page 10: An introduction to node3

ACTUAL SIZE

Node, The Reactor, and You

10

Page 11: An introduction to node3

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

Page 12: An introduction to node3

Example "Time"

Page 13: An introduction to node3

Synchronous Timer

var sleep = require("sleep");

sleep.sleep(

1

);

console.log("NO NO NO");

01.

02.

03.

04.

05.

06.

13

Page 14: An introduction to node3

Asynchronous Timer

setTimeout(

function() {},

1000

);

console.log("GO GO GO");

01.

02.

03.

04.

05.

06.

14

Page 15: An introduction to node3

Callbacks

15

Page 16: An introduction to node3

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

Page 17: An introduction to node3

Phoning in SomeCallbacks

Page 18: An introduction to node3

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

Page 19: An introduction to node3

Node Doesn't CareAbout You

Page 20: An introduction to node3

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

Page 21: An introduction to node3

I Request an HTTPExample

Page 22: An introduction to node3

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

Page 23: An introduction to node3

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

Page 24: An introduction to node3

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

Page 25: An introduction to node3

Half Time Reviewand Questions

Page 26: An introduction to node3

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

Page 27: An introduction to node3

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