nodejs for novices - 28/oct/13 - winnipeg, mb

61
NODE FOR NOVICES BRINGING JAVASCRIPT TO THE SERVER An original presentation by / David Wesst @davidwesst PRESENTED AT THE WINNIPEG .NET USER GROUP - OCTOBER 28, 2013 http://www.winnipegdotnet.org

Upload: david-wesst

Post on 06-May-2015

522 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

NODE FOR NOVICESBRINGING JAVASCRIPT TO THE SERVER

An original presentation by / David Wesst @davidwesst

PRESENTED AT THE WINNIPEG .NET USER GROUP - OCTOBER 28, 2013

http://www.winnipegdotnet.org

Page 2: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

DAVID WESSTFROM WINNIPEG, MANITOBA

SYSTEMS ANALYST, UNIVERSITY OF MANITOBA (FACULTY OFMEDICINE)

MICROSOFT MVP, INTERNET EXPLORER

IE USER AGENT USERAGENTS.IE

PRODUCER, BREWPUB STUDIOS

Page 3: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

DAVID WESST@DAVIDWESST ON TWITTER

DAVIDWESST.COM ON THE WEB

DAVIDWESST.COM/BLOG ON THE BLOGOSPHERE

DW [AT] DAVIDWESST.COM ON EMAIL

Page 4: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

FOLLOW ALONGHTTP://DAVIDWESST.GITHUB.IO/DW-PRESENTATIONS

Page 5: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

SLIDES AND DEMOS WILLBE MADE AVAILABLE

AFTERWARDS

Page 6: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

THE POINTTO ANSWER ONE OF THE FOLLOWING QUESTIONS:

What is Node (a.k.a. NodeJS)?What do I need to get started?What makes NodeJS different to ASP.NET?Why aren't my JS frameworks working in NodeJS?

Page 7: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

THE POINTTHEME FOR THIS PRESENTATION

UNDERSTAND WHAT YOUCAN DO WITH NODEJS

Page 8: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

THE PLAN (A.K.A. THE AGENDA)How are we going to do this?

What is NodeJS?

Getting Yourself Setup

Your NodeJS Toolbox

Getting Your Node On

Page 9: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

THE PLANSETTING YOUR EXPECTATIONS

This presentation is...

An introduction to NodeJSA dialogue about NodeJSComparing NodeJS to ASP.NETA review of NodeJS development toolsA "core sample" of NodeJS fundamentals

Page 10: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

WHAT IS NODEJS?

Page 11: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

WHAT IS NODEJS?

"NODE" = "NODEJS"

Page 12: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

WHAT IS NODEJS?

Node is...

Node.js is a platform built on Chrome'sJavaScript runtime for easily building fast,

scalable network applications. Node.js uses anevent-driven, non-blocking I/O model that

makes it lightweight and efficient, perfect fordata-intensive real-time applications that run

across distributed devices.

Page 13: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

WHAT IS NODEJS?

Node is...

...Server-Side JavaScript

...100% Multi-Platform

...Open Source

...Sponsored by Joyent

Page 14: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

WHAT IS NODEJS?

Node is...

...Powered by Google's V8 JavaScript Engine

...Provides request-level control

...bundled with a package manager (a.k.a npm, node packagedmodules)...Built on the idea of Asynchronous I/O [or Non-Blocking I/O]

Page 15: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

ASYNCHRONOUS I/O?I/O THAT DOESN'T BLOCK YOUR APPLICATION

Page 16: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

YOU: I DON'TUNDERSTAND HOW THAT

IS POSSIBLE. TELL MEMORE!

I WILL.

Page 17: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

EXAMPLE: CLIENT-SIDE JAVASCRIPT

// function with parameterfunction myClientFunction(message, callback) { // variable var msg = message; // objects var randomArray = new Array(); // function call alert(msg); // call the callback callback(); };

var theCallback = function() { //...do something}

myClientFunction(message, theCallback);

Page 18: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

EXAMPLE: SERVER-SIDE (NODEJS)JAVASCRIPT

// function with parameterfunction myServerFunction(message, callback) { // variable var msg = message; // objects var randomArray = new Array(); // function call console.log(msg); // call the callback callback(); };

var theCallback = function() { //...do something}

myServerFunction(message, theCallback);

Page 19: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

THE SECRET IS...

CALLBACKS

Page 20: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

CALLBACKSCALLBACK ARE THE SECRET TO NON-BLOCKING OPERATIONS

On the client, UI is kept unblockedOn the server, Requests are unblocked

Page 21: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

OPTIONAL DEMO:CALLBACKS

Page 22: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

WHERE IS NODE BEING USED INPRODUCTION?

REAL EXAMPLESNodejitsu, Cloud PlatformLinkedInEbay

Page 23: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

LET'S TALK NODEJS ANDJAVASCRIPT

FRAMEWORKS (LIKEBACKBONEJS)

Page 24: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

GETTING YOURSELFSETUP

Page 25: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

WHAT ARE YOU GOING TO NEED?

Your development machine (Windows, Linux, or Mac)An Internet Connection(OPTIONAL) A bit of comfort with a command line

Page 26: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

HOW ARE YOU GOING TO DO IT?WINDOWS AND MAC

1. Go to 2. Download the installer3. Follow the install wizard4. Test it in the CLI (command line interface)

NodeJS.org

Page 27: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

YOUR NODEJS TOOLBOX

Page 28: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

CORE TOOLBOX: TEXT EDITOR + TERMINALTHIS IS PRETTY GOOD TO GET YOU GOING

ViNotepadGEditTextMate

Page 29: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

NODEJS TOOLS: NODECLIPSE / NODE IDEHTTP://WWW.NODECLIPSE.ORG/

Eclipse Plugin(s)Free and Open SourceAdds NodeJS running and debugging support in EclipseVery young project, but provides what you need

Page 30: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

NODEJS TOOLS: CLOUD9HTTP://C9.IO

Hosted IDEFree and Paid OptionsOpen SourceProvides support for major web platformsIncludes other common development features, like MySQLand Terminal

Page 31: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

NODEJS TOOLS: WEBMATRIXHTTP://WWW.MICROSOFT.COM/WEB/WEBMATRIX/

Windows Application from MicrosoftFreeEnvironment for popular web platforms (Node, Rails, PHP,HTML5, ASP.NET)Meant for development of Web Sites over Web Apps

Page 32: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

NODEJS TOOLSWebStorm IDE by JetBrainsNetbeans Plugin Visual Node (Private Beta) by Redgate

LinkLink

Link

Page 33: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

GETTING YOUR NODE ON

Page 34: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

WARMING UP TO NODENODE PROJECTS ARE MADE UP OF THE FOLLOWING:

"App.js" - Your Main Executable File"Package.json" (OPTIONAL) - Describes details of yourapplication"node_modules/" (OPTIONAL) - Contains projectdependencies

Page 35: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

DEMOINTRODUCING NODEJS

Page 36: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

USING THE NPMNODE PACKAGED MODULES

NPM is bundled with Node base install as of v0.6.3Packages can be browsed at Installs packages either locally

...or globally

NPMjs.org

npm install underscore

npm install -g typescript

Page 37: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

PACKAGE.JSON: REVISITEDTHE KEYS TO USING THE NPM

You can...

Define dependencies for running your applicationDefine dependencies for developing your applicationSet the name, version, and main for your own apps andpackagesPublish your own packages to the npm

Page 38: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

DEMO: USING NPMCOMMAND LINE

Page 39: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

PRO TIP

Page 40: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

GITHUB IS YOUR FRIEND

Page 41: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

CODING WITH CALLBACKSUSING THE REAL POWER OF NODEJS

Keys to remember:

Functions are first-class citizens in JavaScriptFunctions can be passed as argumentsDefining functions inside of functions makes them "private"Non-blocking means user waits less, which is good!

Page 42: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

CODING WITH CALLBACKS

Page 43: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

DEMOCODING WITH CALLBACKS

Page 44: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

MAKING YOUR OWN MODULESMODULARITY FOR THE WIN

Keys to remember:

Modules provide a way to easily organize your codeCan be shared amongst other projectsPublished through npm or git repositoryVersion is defined in package.json

Page 45: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

DEMOMAKING YOUR OWN MODULES

Page 46: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

THINGS TO REMEMBERPrivate and Public variables existThink ModularNPM is your friend...and so is Github

Page 47: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

LET'S TALK NODEJS ANDMICROSOFT STUFF

Page 48: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

NODEJS AND MICROSOFTWebsites and Cloud Services support in Windows AzureIDE built into WebMatrixIIS and IIS Support through IISNode

Page 49: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

ASP.NET VS NODEJSServes up JavaScript and HTMLNatively SynchronousRequires IISProvides multiple structured ways of doing web (Web Forms,MVC, Websites, WebAPI)

Page 50: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

ASP.NET VS NODEJSServes up JavaScript and HTMLNatively AsynchronousDoes not require web server (e.g. IIS)Provides multiple unstructured ways of doing webOpen Source

Page 51: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

PRO TIP

Page 52: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

EXPRESSJS: YOUR NEW FAVOURITE NODEMODULE

POPULAR MVC FRAMEWORK FOR NODEProvides MVC Pattern for Web ApplicationsSupports Popular Middle ModulesCommand-Line SetupSupports popular UI tools (e.g. LESS, SASS, Jade Templates)

Page 53: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

DEMOINTRODUCTION EXPRESSJS

Page 54: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

THE PLAN (A.K.A. THE AGENDA)How are we going to do this?

What is NodeJS?

Getting Yourself Setup

Your NodeJS Toolbox

Getting Your Node On

Page 55: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

THE POINTTO ANSWER ONE OF THE FOLLOWING QUESTIONS:

What is Node (a.k.a. NodeJS)?What do I need to get started?What makes NodeJS different to ASP.NET?Why aren't my JS frameworks working in NodeJS?

Page 56: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

THE POINTTHEME FOR THIS PRESENTATION

UNDERSTAND WHAT YOUCAN DO WITH NODEJS

Page 57: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

NEXT STEPSWhere do we go from here?

Page 58: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

NEXT STEPSInstall NodeJSCreate a NodeJS Application with or without ExpressJSExplore the npmPublish a package to the npm

Page 59: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

NEXT STEPSCHECK OUT A RESOURCE

http://package.json.nodejitsu.com/http://blog.nodejitsu.com/npm-cheatsheethttp://npmjs.org

Page 60: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

DAVID WESSTFROM WINNIPEG, MANITOBA

SYSTEMS ANALYST, UNIVERSITY OF MANITOBA (FACULTY OFMEDICINE)

MICROSOFT MVP, INTERNET EXPLORERIE USER AGENT USERAGENTS.IE

PRODUCER, BREWPUB STUDIOS

Page 61: NodeJS for Novices - 28/Oct/13 - Winnipeg, MB

DAVID WESST@DAVIDWESST ON TWITTER

DAVIDWESST.COM ON THE WEBDAVIDWESST.COM/BLOG ON THE BLOGOSPHERE

DW [AT] DAVIDWESST.COM ON EMAIL