Hello world - intro to node js

Post on 13-Jan-2015

2.072 views 1 download

Tags:

description

Jeff Andersen from GoInstant Have you ever thought that writing web applications should allow you to use your mad Javascript skillz on the server side as well? Node.js is such a platform. Bundling up the Google Chrome Javascript runtime, Node lets you easily building fast and scalable network applications perfect for the real-time web. It's also a pretty great platform for building basic data driven websites too. Jeff, a web developer at Halifax based GoInstant, will introduce us to the Node platform, exploring it from the ground up.

Transcript of Hello world - intro to node js

HELLO WORLD

Jeff AndersenDeveloperTwitter: @jeffandersen Email: jeff@goinstant.com

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

Who uses it?

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

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

• Type out commands and hit enter

Use cases• REST APIs

• Streaming data

• High concurrency

• Leveraging UNIX tools

• Realtime applications

Event Driven

Event Driven

Sync vs Async

Synchronous (Blocking)

Asynchronous (Non-Blocking)

Asynchronous (Non-Blocking)

• Node is built for async i/o

• Sometimes difficult to understand

• Callback soup

Let’s run it

HTTP Server

5 lines of codeLet’s run it

HTTP Server

Community / Ecosystem

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

• 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

Package.json

npm install

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

Example app

Example appBasic REST api

What do we need?• HTTP server

• Postgres database

• Postgres client

• JSON formatted responses

We have an HTTP server

Problems?• One handler replying to every request

• Missing

• Routing/URL parameters

• Response formatting

• 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

Same functionality

Let’s code

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

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