Welcome!

Linux Authors: Arsalan Farooq, Maureen O'Gara, Hovhannes Avoyan, David Dodd, Aaron Godfrey

Blog Feed Post

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 website 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');
}).listen(8080);
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 here 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 https://github.com/joyent/node.git
cd node
mkdir ~/local # change to desired installation path
./configure –prefix=$HOME/local/node
make
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.

 

Share Now:del.icio.usDiggFacebookLinkedInBlinkListDZoneGoogle BookmarksRedditStumbleUponTwitterRSS

Read the original blog entry...

More Stories By Hovhannes Avoyan

Hovhannes Avoyan is the CEO of Monitis, Inc., a provider of on-demand systems management and monitoring software to 50,000 users spanning small businesses and Fortune 500 companies.

Prior to Monitis, he served as General Manager and Director of Development at prominent web portal Lycos Europe, where he grew the Lycos Armenia group from 30 people to over 200, making it the company's largest development center. Prior to Lycos, Avoyan was VP of Technology at Brience, Inc. (based in San Francisco and acquired by Syniverse), which delivered mobile internet content solutions to companies like Cisco, Ingram Micro, Washington Mutual, Wyndham Hotels , T-Mobile , and CNN. Prior to that, he served as the founder and CEO of CEDIT ltd., which was acquired by Brience. A 24 year veteran of the software industry, he also runs Sourcio cjsc, an IT consulting company and startup incubator specializing in web 2.0 products and open-source technologies.

Hovhannes is a senior lecturer at the American Univeristy of Armenia and has been a visiting lecturer at San Francisco State University. He is a graduate of Bertelsmann University.