introduction to node.js

44
Introduction to Node.js Please rate this talk: http://spkr8.com/t/19141 Monday, January 14, 13

Upload: troy-miles

Post on 08-May-2015

738 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Introduction to Node.js

Introduction to Node.js Please rate this talk: http://spkr8.com/t/19141

Monday, January 14, 13

Page 2: Introduction to Node.js

Our Goals• What Node Isn't and Is

• How to Install

• Hello World - Node Style

• Node Workflow

• Three Key Ideas

• Introducing NPM

• A Website

• A RESTful Web Service

• Why Node

• Resources

• Summary

Monday, January 14, 13

Page 3: Introduction to Node.js

Disclaimer

The opinions expressed in this talk are my own and don’t represent those of my employer, my friends, my family, or even me.

Monday, January 14, 13

Page 4: Introduction to Node.js

Who am I?I am a Microsoft Certified Solution Developer and I’ve been developing software since 1979. Since 2009, I have been focused on developing mobile applications, for  iPhone, Android, the mobile web, and Windows Phone 7.

Monday, January 14, 13

Page 5: Introduction to Node.js

Who Are You? (I hope)

• Experienced with JavaScript

• Experience with some other server framework

• Familiar with the Unix Tool Chain

• Familiar with Git

Monday, January 14, 13

Page 6: Introduction to Node.js

What Node Isn't and Is?

• What Node Isn't?

• What Node Is?

• How to Spell It?

Monday, January 14, 13

Page 7: Introduction to Node.js

What Node Isn't?

Monday, January 14, 13

Page 8: Introduction to Node.js

What Node Is?

• The Official Answer

• It is built on Google's V8

• The Server and the App Are One

Monday, January 14, 13

Page 9: Introduction to Node.js

The Official Answer

• Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Monday, January 14, 13

Page 10: Introduction to Node.js

It is built on Google's V8

• Google’s Open Source JavaScript Engine

• V8 is really fast

• It is compiled, sort of

Monday, January 14, 13

Page 11: Introduction to Node.js

The Server and The App Are One

• Unlike Other Technologies

• IIS and ASP.NET ( aspnet_wp.exe)

• Apache HTTP and PHP

• Complete Control of the HTTP Request

Monday, January 14, 13

Page 12: Introduction to Node.js

How to Spell It?

• Node.js

• Node.JS

• Node (my preferred spelling)

Monday, January 14, 13

Page 13: Introduction to Node.js

How to Install

• http://nodejs.org/downloads

• Mac/PC/Linux/SunOS

• Azure

• http://www.windowsazure.com/en-us/develop/nodejs/

Monday, January 14, 13

Page 14: Introduction to Node.js

Hello World Node Style

var http=require('http');

http.createServer(function (req, res) {

res.writeHead(200, { 'Content-Type': 'text/plain});

res.end('Hello World\n');

}).listen(3000);

console.log('Server running at http://localhost:3000/');

Monday, January 14, 13

Page 15: Introduction to Node.js

A Slightly Better Hello World

var http=require('http');

var server = http.createServer();

server.on(function (req, res) {

res.writeHead(200, { 'Content-Type': 'text/plain});

res.end('Hello World\n');

}).listen(3000);

console.log('Server running at http://localhost:3000/');

Monday, January 14, 13

Page 16: Introduction to Node.js

Node Workflow

• The REPL

• Developing

• Publishing

Monday, January 14, 13

Page 17: Introduction to Node.js

The REPL

• Read - Eval - Print Loop

• Brings JavaScript to the command line

• Allows all JavaScript commands

• Isn’t Really too useful

Monday, January 14, 13

Page 18: Introduction to Node.js

Developing

• Text Editor / Terminal

• VIM

• WebStorm

Monday, January 14, 13

Page 19: Introduction to Node.js

Publishing with Git

• Git

• Microsoft Azure

• Heroku

Monday, January 14, 13

Page 20: Introduction to Node.js

Three Key Ideas

• Callbacks

• Events

• Modules

Monday, January 14, 13

Page 21: Introduction to Node.js

Callbacks

• Callbacks are the key to Asynchronous Programming

• Avoid thinking in Java or C# with callbacks

query(“SELECT * from db”, function(result) {

/* do something with result */

});

Monday, January 14, 13

Page 22: Introduction to Node.js

Events

• Events are Core to Node’s Architecture

• Events are defined in the Module, Events

• Events are hooked using .on

• Events are triggered using .emit

• Your code can define and emit events also

Monday, January 14, 13

Page 23: Introduction to Node.js

Modules

• Based on CommonJS

• Solves the Issue of the JavaScript Global Object

• Modules are wrapped in anonymous functions

Monday, January 14, 13

Page 24: Introduction to Node.js

Modules(function() {

/* contents of module file */

})();

Monday, January 14, 13

Page 25: Introduction to Node.js

Introducing NPM

• Node Package Manager

• http://npmjs.org

• Core

• Userland

• Types of Installs

• Don’t Re-invent the Wheel

Monday, January 14, 13

Page 26: Introduction to Node.js

Core

• Packages that are internal to Node

• Defined in Node's source in the lib/ folder

• Modules like: http, util, fs, etc.

Monday, January 14, 13

Page 27: Introduction to Node.js

Userland

• Modules loaded from NPM or other

• npm install <module name>

• npm install -g <module name>

Monday, January 14, 13

Page 28: Introduction to Node.js

Types of Installs

• Global - Accessible to all Node Apps

• Local - Accessible only to the current App

• Prefer Local

• App has all components when published

• No need to sudo

Monday, January 14, 13

Page 29: Introduction to Node.js

Don’t Re-invent the Wheel

• There are over 20k Packages already defined

• Most of your problems have already been solved

Monday, January 14, 13

Page 30: Introduction to Node.js

Top Ten Packages

• #10 connectConnect is an extensible HTTP server framework for node, providing high performance "plugins" known as middleware. Connect is bundled with over 20 commonly used middleware, including a logger, session support, cookie parser, and more.

Monday, January 14, 13

Page 31: Introduction to Node.js

Top Ten Packages

• #9 coffee-scriptCoffeeScript is a little language that compiles into JavaScript.

Monday, January 14, 13

Page 32: Introduction to Node.js

Top Ten Packages

• #8 underscoreUnderscore.js is a utility-belt library for JavaScript that provides support for the usual functional suspects (each, map, reduce, filter...) without extending any core JavaScript objects.

Monday, January 14, 13

Page 33: Introduction to Node.js

Top Ten Packages

• #7 jadeJade is a high performance template engine heavily influenced by Haml and implemented with JavaScript for node.

Monday, January 14, 13

Page 34: Introduction to Node.js

Top Ten Packages

• #6 redisThis is a complete Redis client for node.js. It supports all Redis commands, including many recently added commands like EVAL from experimental Redis server branches.

Monday, January 14, 13

Page 35: Introduction to Node.js

Top Ten Packages

• #5 mochaMocha is a simple, flexible, fun JavaScript test framework for node.js and the browser.

Monday, January 14, 13

Page 36: Introduction to Node.js

Top Ten Packages

• #4 socket.ioSocket.IO is a Node.JS project that makes WebSockets and realtime possible in all browsers. It also enhances WebSockets by providing built-in multiplexing, horizontal scalability, automatic JSON encoding/decoding, and more.

Monday, January 14, 13

Page 37: Introduction to Node.js

Top Ten Packages

• #3 asyncAsync is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. Although originally designed for use with node.js, it can also be used directly in the browser.

Monday, January 14, 13

Page 38: Introduction to Node.js

Top Ten Packages

• #2 requestSimplified HTTP request client.

Monday, January 14, 13

Page 39: Introduction to Node.js

Top Ten Packages

• #1 ExpressFast, unopinionated, minimalist web framework for node.

Monday, January 14, 13

Page 40: Introduction to Node.js

A Website

Monday, January 14, 13

Page 41: Introduction to Node.js

A RESTful Web Service

Monday, January 14, 13

Page 42: Introduction to Node.js

Why Node?

• Avoids the Web Dev Context Switch

• Gives You Full Control of the Server

• Makes Asynchronous Coding Easy

• Node is Fun

Monday, January 14, 13

Page 43: Introduction to Node.js

Resources

• http://nodejs.org

• http://npmjs.org

• http://nodetuts.com

• http://howtonode.org

• http://package.json.nodejitsu.com

Monday, January 14, 13

Page 44: Introduction to Node.js

Summary• What Node Isn't and Is

• How to Install

• Hello World - Node Style

• Node Workflow

• Three Key Ideas

• Introducing NPM

• A Website Using Packages

• A RESTful Web Service

• Why Node

• Resources

• Summary

Monday, January 14, 13