Notes‎ > ‎

George on Node

What is Node?

Node is a platform for writing realtime web apps in Javascript designed to handle large numbers of simultaneous micro-requests that would break Ajax-based web apps served by PHP.

Node is not a JavaScript library like jQuery that runs as part of a website. Node apps are standalone servers that replace Apache or Nginx using a single process, not threads.

JavaScript is to Node like SQL is to MySQL, a way to tell Node what to do.

Node tells the operating system (through epoll, kqueue, /dev/poll, or select) that it should be notified when a new connection is made, and then it goes to sleep. If someone new connects, then it executes the callback. Each connection is only a small heap allocation, much more efficient than threads.

Node uses synchronous events and asynchronous callbacks, event-driven non-blocking I/O like Twisted for Python, EventMachine for Ruby or POE for Perl.

The main advantage to Node: Every line of code returns instantly (you never wait for things like a database query to complete). Everything runs in parallel, except your code.

To understand this important point, think of your code as the King and Node as your army of servants. You (the King) tell each servant what to do, like run a database query or read a file, and each servant will work on his task in parallel, but will deliver the result one at a time in the order the tasks complete, not in the order they are issued.

Node is written in C++ and built on V8, Google's open source JavaScript engine. V8 compiles JavaScript to native machine code before executing it. V8 does not execute bytecode or interpret JavaScript.

Node website

V8 website

V8 on Wikipedia

Important Node Links

Node docs

Node modules

Node Beginner Book

Node on O'Reilly

Node for Windows


Install Node

Click "Download" and choose "Windows"

Node installs here (you cannot change this):
C:\Program Files (x86)\nodejs\

After installation, open a cmd.exe prompt and type "node"

Doesn't work?

Confirm your path includes: "C:\Program Files (x86)\nodejs\"
(I had to manually add it to Control Panel > System > User variables > Path)

Run Node Apps

Run "node" from the package root, not a subfolder

eg

Works:
node bin/server

(Had to edit package.json file to change "express": "v2.4.7" to "express": "v2" because [email protected] does not support [email protected])

Does not work:
cd bin
node server

(Opening localhost:8080 throws Error: failed to locate view "videos.ejs")

Upgrade Node


Node

Overwrite C:\Program Files (x86)\nodejs\node.exe

Sources


NPM - Node Package Manager

Comes with Node at
C:\Program Files (x86)\nodejs\node_modules\npm


npm install .

Install all dependencies (run from package root)


npm ls 

Module list
Shows all modules in current package (reads the node_modules folder)


npm update npm -g

Upgrade NPM


npm install $name

Install new package

First downloads to cache:
C:\Users\george\AppData\Roaming\npm-cache

Then unzips to:
node_modules/

Local vs Global Install

Local install (default) adds to ./node_modules in the current package root
Install locally if you are going to require() it

Never use shared global modules!

"The shift from global module installs to local ones sets node apart from previous generation platforms. Ruby and Python fail horribly in this arena and the fact that it is now standard practice to develop and deploy in to entirely sandboxed environments of the entire platform (virtualenv, rvm) is admission of that failure."

Also, check in "node_modules" for apps you deploy, not reusable packages you maintain.


Global install (with -g) adds to /usr/local or wherever node is installed
Install globally if you are going to run it on the command line

On Windows
npm install formidable -g

Installs into:
C:\Users\george\AppData\Roaming\npm\node_modules\formidable

Even if you run cmd.exe as Administrator.

? Why not into:
C:\Program Files (x86)\nodejs\node_modules\formidable

Local Install

Tries to find the package $root folder if you run npm from a subfolder by walking up the tree checking for a package.json file or a node_modules folder.

If no package root is found, then the current folder is used as $root.

Package $package is unpacked into
$root/node_modules/$package

Dependencies are unpacked into
$root/node_modules/$package/node_modules

NPM avoids dependency cycles by installing only what is necessary



GitHub


GitHub for Windows

Install GitHub for Windows

TGN GitHub

Visit TGN on GitHub

Note: Sign in to GitHub for the "Clone in Windows" button to work

Cloud9 IDE

Best online code editor for realtime collaborative coding

Sign in with GitHub to avoid YAA (yet another account)

Baby Steps


Hello World

Source

Run: node server

Hello Modules

Modules are like Classes in C++


Run: node index

Hello Router

Proper router pattern

Source


About Node


No Web Server Required

Node is a web server and a web development platform
It replaces Apache or Nginx


Event-Driven Non-Blocking I/O

Every line of code returns instantly
An asynchronous callback function gets the result when it is ready


Everything runs in parallel, except your code

Only one callback executes at a time.

Analogy: Your code is the King and Node is your army of servants.

You (the King) tell each servant what to do, like run a database query or read a file, and each servant will work on his task in parallel, but each will line up to deliver the result one at a time so you can focus.


Node is FAST

JavaScript is a universal language, and V8 (Google's open-source Javascript engine) is one of the fastest dynamic language interpreters on the planet. No other language is being pushed for speed as aggressively as JavaScript is right now.

Also, Node's I/O facilities are really light weight, bringing you as close to fully utilizing your system's full I/O capacities as possible.


Example: PHP vs. Node

// PHP - "Hello World" appears after database query completes
var result = database.query("SELECT * FROM hugetable");
console.log("Hello World");

// Node - "Hello World" appears instantly
database.query("SELECT * FROM hugetable", function(rows) {
  var result = rows;
});
console.log("Hello World");


Single Thread

Everything executes in a single thread
An event loop queues all requests

In PHP, each HTTP request gets its own blocking thread
In Node, each HTTP request shares the same non-blocking thread


Multi-Threaded by Choice

Processes started by child_process.fork() are scheduled in parallel.

Use the cluster module for load balancing incoming connections across multiple processes.

Comments