@rob_rich
#nodejs

Welcome
to Node.js:

a pragmatic introduction


by Rob Richardson

@rob_rich

http://robrich.org/

August 29, 2013

About Me

Rob Richardson is principal of Richardson & Sons, LLC, builder of ASP.NET and Node solutions for small- to medium-sized businesses. Rob has a BFA and MSCIS, and has been building software in .NET and HTML for over a dozen years, and frequently enjoys sharing his passion for software development at user groups and community events. Follow Rob at @rob_rich or on his blog at http://robrich.org/.

What is Node?

JavaScript runtime

Powered by Google V8
(the JavaScript engine in Chrome)

Runs on Windows and *nix

What to do with Node?

Very scalable webserver

Command-line tools

Why Node?

Very, very fast for I/O bound tasks

Single language for both browser and server-side

Methodologies of Node

Everything is asynchronous, no blocking

Standard JavaScript

No global scope

Community of open-source packages

Installing Node

Goto http://nodejs.org/

Click Install

Push Next

Open Command Prompt / Terminal

node --version

The Node REPL

node

read-eval-print loop

Run a Node App

node app.js

Everything is Asynchronous

There is no Thread.Sleep()

There is setTimeout(..)

Everything takes in a callback to return results

Event loop is free to handle other requests

Everything is Asynchronous

  1. Pass in a callback as the last argument to the function
  2. The first argument to your callback is an error (if any)


callTheLib('pass','parameters', function (err, results) {
	if (err) {
		throw err; // Bad things happened
	}
	// It worked
});
						

Hello Web Server

Source: http://nodejs.org/


var http = require('http');
var port = process.env.PORT || 1337;

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(port);

console.log('Server running at http://127.0.0.1:'+port+'/');
						

Module Loading: Core Modules

Load modules into your code using CommonJS paradigm

(synchronous)


var lib_instance = require('lib_name');
						

Module Loading: External Modules

Install package with NPM (Node Package Manager)


npm install packagename
						

Load modules into your code


var lib_instance = require('packagename');
						

Hello Web Server


var http = require('http');
var port = process.env.PORT || 1337;

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(port);

console.log('Server running at http://127.0.0.1:'+port+'/');
						

Hello Web Server

Interesting, but not that useful

We prefer web frameworks

Enter Express: an MVC framework for Node

Hello Express

Source: http://expressjs.com/guide.html


npm install -g express
express myapp
cd myapp
npm install
node app.js
						

Command-line tools with Node

JSHint: detect errors in JavaScript code

Grunt: a JavaScript build tool

Debugging Node Apps

node-inspector: Uses Google Developer Tools to attach to Node's V8 engine.

Tutorials: here and here.

WebStorm: an HTML5/CSS/JavaScript IDE by JetBrains

Tutorials: here and here.

Glossary of .NET and Node.js

.NET Node
Windows Windows, *nix
IIS Node.exe
ASP.NET Node Core Modules
ASP.NET MVC Express (Like Sinatra)
Razor Jade
NuGet NPM (Installed with Node)
SignalR Socket.io
Visual Studio WebStorm or
Sublime Text or vi
C# JavaScript

Questions?

@rob_rich

http://robrich.org/