Getting Started with Node.js


There is an almost insurmountable myriad of server-side environments and web servers all aiming to accomplish the same task: doing the most work in the most efficient way in the least amount of time. However, there are few of these technologies which arguably conquer the many road blocks to achieving this goal (namely concurrency issues and handling the overhead of connection allocation). Among these select few is Node.js.

Node.js, in its purest form, is a server-side environment built on top of Google’s V8 Javascript Engine. It is geared primarily toward the facilitation of network and server programming, keeping both ease of use, speed, and capability in mind. In fact, the Node.js demonstrates just how simply you can get up and running with a respectable HTTP server “Hello, World” example:

var http = require('http');
http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');
console.log('Server up and running!');


But do not be too quick to question Node’s aptitude based off of its simplicity, for it has some clever tricks up its sleeves.

One of the most crucial and fundamental features of Node.js is its broadened use of callback functions. Node uses this feature to conquer our first road block of concurrency issues, seamlessly eliminating many common frustrations involved in the writing of asynchronous code. To do this, all of Node’s built-in modules for things such as I/O and socket handling is implicitly asynchronous and uses the callback paradigm, triggering callbacks as end events.

Node also conquers our second road block of reducing the overhead of allocating memory for multiple threads. In fact, the beauty of it all—and most surprisingly—is that Node.js runs everything from a single-threaded event loop which comes with some great advantages over classical multi-threaded designs.

Getting up & running

Node.js is by far one of the quickest server-side environments to get up and running. The following instructions are brevities of the steps described here:

1) Go to the Node.js website and follow the links to the download.
2) If you’re running on Windows, you will need a shell tool such as Cygwin. You will also need to get a compiler, such as gcc or Xcode (for Macs) if you plan on building Node from source (there are some pre-built binary distros for Windows machines and via popular package managers for Mac or Linux machines here).
3) From the command line, use the following commands to build and install Node:

git clone --depth 1
cd node
mkdir ~/local # change to desired installation path
./configure –prefix=$HOME/local/node
make install
export PATH=$HOME/local/node/bin:$PATH


After that, you’re done! Create a new Javascript file named something like “server.js” using the Hello, World code as an example script, then execute node /path/to/server.js , and you’ve now successfully started a simple Node.js HTTP server which you can access in your browser at http://localhost:8080 (or wherever you are running the server from).

The Node.js module system

Node uses a fairly useful module-based system to provide “include”-esque functionality most comparable to CommonJS’s implementation. In the Hello, World code, var http = require(‘http’); is used to load the “http” module and store the reference to this module in our variable aptly named http (using the same name for the variable as the module is not required of course). The syntax is the same for all of the Node.js core modules (net, fs, sys, etc).

Node also allows for writing custom modules using a similar syntax (though with a different inclusion path) such that var my_module = require(‘./my_module’); will load and include the “./my_module.js“ file. Specifics on building custom modules will be discussed in a later chapter.

Banner 468 x 60 copy


You might also like