Node Basics

Still relatively early on in its development, Node.js is yet not clearly defined to outsiders, even as its early adherents wax enthusiastic about the technology.

Node is a runtime environment for JavaScript, built on Google's V8 JavaScript engine. JavaScript, at least historically, was run in the browser (each browser included a JavaScript engine). Node, however, is run on the server.

Downloading Node, you get a command-line interface and access to a complete library of modules, accessable through the npm (node package manager).

It is important to understand that Node.js uses an event-driven, non-blocking I/O model.

In practical terms, this model affects how you structure your code. Each block of code is executed simultaneously. For starters, any function that relies on data provided by another function must be embedded within the function that supplies the data. See the example below of asynchronously reading a file.

The best place I've found to learn about Node is Max Odgen's "A Short Introduction to Node." The most valuable thing about Node, Odgen notes, is that it is an I/O platform, best used for running "I/O based programs that need to be fast and/or handle lots of connections":

Max Odgen's Node IO diagram

The NodeSchool site offers modules that you can download and run on on Node itelf to learn how Node works, starting with LearnYouNode.

Node Code Snippets

  • Parsing the command line
  • Summing values provided by the command line
  • Reading a file
  • Reading a file asynchronously
  • Read the contents of a directory
  • Deleting a file
  • Running a Node module
  • Embed a function within another function
  • Callbacks
  • Fetching data from a MySQL database
  • Back