intro to node

25
Intro to Node.JS By Aaron Stannard Startup Developer Evangelist, Microsoft Corporation

Upload: aaron-stannard

Post on 17-May-2015

8.967 views

Category:

Technology


2 download

DESCRIPTION

Node.JS is a popular server-side JavaScript framework for handling real-time and distributed data processing. In this session you'll learn what Node.JS is, how it works under the hood, and what scenarios it's useful for. You'll also learn how to deploy it to Windows Azure and manage it inside of IIS 7.

TRANSCRIPT

Page 1: Intro to Node

Intro to Node.JS

By Aaron StannardStartup Developer Evangelist, Microsoft Corporation

Page 2: Intro to Node

Examples from Today’s Talk

https://github.com/Aaronontheweb/introtonode

Page 3: Intro to Node

Meet Node

var http = require('http');

//Node function called each time a new HTTP request arrivesfunction onRequest(req, res){ res.writeHead(200, {'Content-Type':'text/plain'}); res.end('Hello ' + req.connection.remoteAddress + '!'); /* Write the IP addresses of our connecting client to console */ console.log('Incoming connection from ' + req.connection.remoteAddress);}

//Listen for connections on the port provided by the host processvar server = http.createServer(onRequest).listen(process.env.PORT);

Page 4: Intro to Node

What Is Node?

• Server Side JavaScript• Asynchronous Web Framework• Evented I/O Model

Page 5: Intro to Node

What Is Node?

Network I/O Libraries

+

+

=

Page 6: Intro to Node

What Makes Node Special?

• Uses the familiar JavaScript programming model to enable server-side development

• Buries the pain of working with async / parallel programming

• No Blocking I/O• Can handle large concurrent loads more easily

than other technologies• Not a silver bullet

Page 7: Intro to Node

Why Node

JavaScript is ubiquitous

Page 8: Intro to Node

Why Node

Traditional Platforms• Things you need for

concurrent web programming:– Locks– Threads / Thread Pools– Thread Management– Inter-Thread Communication– Callbacks + Enclosures + Sync

to Blocking Calls

Node• Things you need for

concurrent web programming:– Callbacks + Enclosures

Which looks simpler?

Page 9: Intro to Node

Good Node Scenarios

Behavior• High request volume, low

response-sizes• Pub / Sub

• Real-time applications

Application• Online games, message

passing services• Social networks, content-

syndication services• Chat, real-time games

Page 10: Intro to Node

Bad Node Scenarios

Behavior• Large HTTP response sizes

• CRUD• Transaction-heavy systems

Application• Static file servers, reporting

tools• Blogs, CMS• E-Commerce

Page 11: Intro to Node

Node Concepts

• What Do Node Projects Look Like?• Event Loop & Workers• HTTP as a First Class Citizen• Callbacks and Functions as Objects• Modules & Packages

Page 12: Intro to Node

Structure of a Node Project/projectroot/

package.jsonreadme.txtweb/server.jsviews/index.htmlmodels/post.jshelpers/timestamp.jstest/route-test.jspost-test.jsnode_modules/

C:\[projectroot]> node web/server.jsNode server listenening on port 3000 in development mode

Tells Node and NPM what packages are required

The main entry point for your application

User-defined module for handling persistence

User-defined module for other stuff

Root of your actual application

Directory for unit tests

Output directory for all NPM installations

Page 13: Intro to Node

Event Loop + Workers

Client

Clients send HTTP requeststo Node.JS server...

EventLoop

(single thread)

Event loop is woken up by OS.Passes request + responseobjects to worker functionswith callbacks.

Long-running jobsrun on worker threads..

Non-blockingWorker

(internal C++ threadpool)

Responses are sentto main thread viacallback

Event loop returnsresult to client

Page 14: Intro to Node

HTTP

• HTTP is a first class citizen• Request Object– Holds properties about the client– Holds session state– Passes client-data to request handlers

• Response Object– Holds the HTTP response stream– Can be written in chunks

Page 15: Intro to Node

Callbacks and Functions as Objects

// HTTP GET /app/listexports.listapps = function(req, res){

var pageTitle = req.session.userName + "'s Apps";

appProvider.getAppsForUser(req.session.userName, function(error, userApps){

if(error){console.log(error.message);return res.redirect('/');

}

res.render('list_apps', {locals:{title: pageTitle, apps:userApps}})

});}

Page 16: Intro to Node

Modules & Packages

• Including a built-in modulevar http = require('http');

• Including your own module[timestamp.js] exports.currentTime = function(){…}

[server.js] var timestamp = require(‘./helpers/timestamp');[server.js] console.log(timestamp.currentTime());

• Node Package Manager (NPM)[Commandline] C:\> npm install express[server.js] var express = require(‘express’);

Page 17: Intro to Node

Microsoft & Node

Microsoft wants Windows Azure to be an option for developers regardless of stack

=>

Page 18: Intro to Node

Node on Windows Azure

• IISNode• Windows Azure Node PowerShell Cmdlets• Windows Azure Node SDK• Cloud9 IDE

Page 19: Intro to Node

IISNode

• Hosts Node applications in IIS as native modules

• Automatically manages life-cycle of node.exe• Can load-balance multiple node.exe processes

Page 20: Intro to Node

Azure / Node Powershell Cmdlets

• Create new Azure Services• Add Node Web and Worker Roles• Deploy to Azure Emulator• Import Publish Settings• Deploy to Azure (Production / Staging)• Source @ Github

Page 21: Intro to Node

Windows Azure SDK

• Loads Storage Settings from Web.config• Azure Tables• Azure Blobs• Azure Queues

[console] C:\> npm install azure

Source @ Github

Page 22: Intro to Node

Cloud9 IDE

• Browser-based IDE• Deploys to Windows Azure• Only way to deploy without Windows Vista / 7

Page 23: Intro to Node

Popular Node Frameworks and Tools

• Express (MVC)• Connect• Socket.IO• Unit Testing:– Assert (built-in)– Nodeunit

Page 24: Intro to Node

Further Reference

• NodeBeginner.org - Used this to teach myself Node when I was first getting started.

• NodeJS.org - Official Node Homepage• Github.com/WindowsAzure – Azure bits

Page 25: Intro to Node

About Me

• Aaron Stannard– Twitter: @Aaronontheweb– Github: @Aaronontheweb– Blog: http://www.aaronstannard.com/