nodejs for novices - 28/oct/13 - winnipeg, mb
TRANSCRIPT
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
DAVID WESSTFROM WINNIPEG, MANITOBA
SYSTEMS ANALYST, UNIVERSITY OF MANITOBA (FACULTY OFMEDICINE)
MICROSOFT MVP, INTERNET EXPLORER
IE USER AGENT USERAGENTS.IE
PRODUCER, BREWPUB STUDIOS
DAVID WESST@DAVIDWESST ON TWITTER
DAVIDWESST.COM ON THE WEB
DAVIDWESST.COM/BLOG ON THE BLOGOSPHERE
DW [AT] DAVIDWESST.COM ON EMAIL
FOLLOW ALONGHTTP://DAVIDWESST.GITHUB.IO/DW-PRESENTATIONS
SLIDES AND DEMOS WILLBE MADE AVAILABLE
AFTERWARDS
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?
THE POINTTHEME FOR THIS PRESENTATION
UNDERSTAND WHAT YOUCAN DO WITH NODEJS
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
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
WHAT IS NODEJS?
WHAT IS NODEJS?
"NODE" = "NODEJS"
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.
WHAT IS NODEJS?
Node is...
...Server-Side JavaScript
...100% Multi-Platform
...Open Source
...Sponsored by Joyent
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]
ASYNCHRONOUS I/O?I/O THAT DOESN'T BLOCK YOUR APPLICATION
YOU: I DON'TUNDERSTAND HOW THAT
IS POSSIBLE. TELL MEMORE!
I WILL.
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);
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);
THE SECRET IS...
CALLBACKS
CALLBACKSCALLBACK ARE THE SECRET TO NON-BLOCKING OPERATIONS
On the client, UI is kept unblockedOn the server, Requests are unblocked
OPTIONAL DEMO:CALLBACKS
WHERE IS NODE BEING USED INPRODUCTION?
REAL EXAMPLESNodejitsu, Cloud PlatformLinkedInEbay
LET'S TALK NODEJS ANDJAVASCRIPT
FRAMEWORKS (LIKEBACKBONEJS)
GETTING YOURSELFSETUP
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
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
YOUR NODEJS TOOLBOX
CORE TOOLBOX: TEXT EDITOR + TERMINALTHIS IS PRETTY GOOD TO GET YOU GOING
ViNotepadGEditTextMate
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
NODEJS TOOLS: CLOUD9HTTP://C9.IO
Hosted IDEFree and Paid OptionsOpen SourceProvides support for major web platformsIncludes other common development features, like MySQLand Terminal
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
NODEJS TOOLSWebStorm IDE by JetBrainsNetbeans Plugin Visual Node (Private Beta) by Redgate
LinkLink
Link
GETTING YOUR NODE ON
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
DEMOINTRODUCING NODEJS
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
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
DEMO: USING NPMCOMMAND LINE
PRO TIP
GITHUB IS YOUR FRIEND
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!
CODING WITH CALLBACKS
DEMOCODING WITH CALLBACKS
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
DEMOMAKING YOUR OWN MODULES
THINGS TO REMEMBERPrivate and Public variables existThink ModularNPM is your friend...and so is Github
LET'S TALK NODEJS ANDMICROSOFT STUFF
NODEJS AND MICROSOFTWebsites and Cloud Services support in Windows AzureIDE built into WebMatrixIIS and IIS Support through IISNode
ASP.NET VS NODEJSServes up JavaScript and HTMLNatively SynchronousRequires IISProvides multiple structured ways of doing web (Web Forms,MVC, Websites, WebAPI)
ASP.NET VS NODEJSServes up JavaScript and HTMLNatively AsynchronousDoes not require web server (e.g. IIS)Provides multiple unstructured ways of doing webOpen Source
PRO TIP
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)
DEMOINTRODUCTION EXPRESSJS
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
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?
THE POINTTHEME FOR THIS PRESENTATION
UNDERSTAND WHAT YOUCAN DO WITH NODEJS
NEXT STEPSWhere do we go from here?
NEXT STEPSInstall NodeJSCreate a NodeJS Application with or without ExpressJSExplore the npmPublish a package to the npm
NEXT STEPSCHECK OUT A RESOURCE
http://package.json.nodejitsu.com/http://blog.nodejitsu.com/npm-cheatsheethttp://npmjs.org
DAVID WESSTFROM WINNIPEG, MANITOBA
SYSTEMS ANALYST, UNIVERSITY OF MANITOBA (FACULTY OFMEDICINE)
MICROSOFT MVP, INTERNET EXPLORERIE USER AGENT USERAGENTS.IE
PRODUCER, BREWPUB STUDIOS
DAVID WESST@DAVIDWESST ON TWITTER
DAVIDWESST.COM ON THE WEBDAVIDWESST.COM/BLOG ON THE BLOGOSPHERE
DW [AT] DAVIDWESST.COM ON EMAIL