Transcript
Page 1: Hello world - intro to node js
Page 2: Hello world - intro to node js

HELLO WORLD

Page 3: Hello world - intro to node js

Jeff AndersenDeveloperTwitter: @jeffandersen Email: [email protected]

Page 4: Hello world - intro to node js
Page 5: Hello world - intro to node js

What is Node.js?• In a nutshell: server-side JavaScript

• Used to build scalable network applications

• Built on Google’s v8 JavaScript engine

• Event-driven, non-blocking (asynchronous) I/O

Page 6: Hello world - intro to node js

Who uses it?

Page 7: Hello world - intro to node js

Installing Node.js• Visit nodejs.org

• Download the package/installer and run

• Alternatively: use nvm - Node Version Manager (Similar to Ruby’s RVM) https://github.com/creationix/nvm

Page 8: Hello world - intro to node js

Test out the repl• From the command line type `node`

• Type out commands and hit enter

Page 9: Hello world - intro to node js

Use cases• REST APIs

• Streaming data

• High concurrency

• Leveraging UNIX tools

• Realtime applications

Page 10: Hello world - intro to node js

Event Driven

Page 11: Hello world - intro to node js

Event Driven

Page 12: Hello world - intro to node js

Sync vs Async

Page 13: Hello world - intro to node js

Synchronous (Blocking)

Page 14: Hello world - intro to node js

Asynchronous (Non-Blocking)

Page 15: Hello world - intro to node js

Asynchronous (Non-Blocking)

• Node is built for async i/o

• Sometimes difficult to understand

• Callback soup

Page 16: Hello world - intro to node js

Let’s run it

Page 17: Hello world - intro to node js

HTTP Server

Page 18: Hello world - intro to node js

5 lines of codeLet’s run it

Page 19: Hello world - intro to node js

HTTP Server

Page 20: Hello world - intro to node js

Community / Ecosystem

http://notinventedhe.re/on/2011-7-26

Page 21: Hello world - intro to node js

• npmjs.org

• Open source software

• Official package manager for Node.js

• Nearly 55 thousand packages and almost 2.5 million downloads per day

• package.json

Page 22: Hello world - intro to node js

Package.json

Page 23: Hello world - intro to node js

npm install

Page 24: Hello world - intro to node js

Recommended Modules• async - Higher-order functions and common patterns for

asynchronous code

• lodash - A utility library delivering consistency, customization, performance, & extras.

• express - Sinatra inspired web development framework

• mocha - simple, flexible, fun test framework

• pg - PostgreSQL client

• redis - Redis client

Page 25: Hello world - intro to node js

Example app

Page 26: Hello world - intro to node js

Example appBasic REST api

Page 27: Hello world - intro to node js

What do we need?• HTTP server

• Postgres database

• Postgres client

• JSON formatted responses

Page 28: Hello world - intro to node js

We have an HTTP server

Page 29: Hello world - intro to node js

Problems?• One handler replying to every request

• Missing

• Routing/URL parameters

• Response formatting

Page 30: Hello world - intro to node js

• npm install express

• Sinatra style web framework

• Simple verb-based request handlers

• Full-featured router

• body parser, url parameters, query string parsing

• Automatic response formatting

Introducing Express

Page 31: Hello world - intro to node js

Same functionality

Page 32: Hello world - intro to node js

Let’s code

Page 33: Hello world - intro to node js

What could be added?• OAuth middleware

• Middleware: A function that processes a request, typically on an interim basis to the final request handler.

• Permissions & token scopes

• JSON web hooks

Page 34: Hello world - intro to node js

Twitter recommendations• @izs - Isaac Schlueter, Creator of NPM & Node core gate keeper

• @eranhammer - Eran Hammer, Walmart Labs & OSS contributor

• @substack - James Halliday, Node module authoring machine

• @tjholowaychuk - TJ Holowaychuk, Author: Express, Mocha, etc

• @igrigorik - Ilya Grigorik, Developer advocate at Google

• @jeresig - John Resig, Creator of jQuery

• @goinstant - Realtime Collaboration API and Backend

Page 35: Hello world - intro to node js