October 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Introducing Node.js: JavaScript for Massively Concurrent Apps

  • January 24, 2011
  • By Jason Gilmore
  • Send Email »
  • More Articles »

I fondly recall sitting around the dinner table as a young boy, listening to my father and grandfather reminisce about "the old days," a simpler time when men were men, cars were cars, and humanity apparently walked perpetually uphill. I wonder whether in another 30 years if I'll be berating my own grandchildren about the good old days of computing, a time when RAM meant something, darn it!

The early days of the World Wide Web were also a simpler time, an era when developer's were primarily concerned with building websites which served a mix of dynamically and statically generated pages. Install Apache, build a series of interfaces which interact with a database, and the job is done.

With the mainstream Web now a teenager, things have become a bit more complicated. These days the emphasis has moved away from creating websites and toward building Web applications. This goal of creating Web-based applications which closely resemble their desktop-based brethren and involving traffic on a massive, worldwide scale has left developers grappling with a relatively new problem domain involving how to appropriately deal with concurrency.

If you're not familiar with the technical challenges surrounding building massively concurrent applications, you might be surprised to know that JavaScript has emerged as one of the front-running solutions helping to solve these challenges. Yes, JavaScript. As it turns out, JavaScript's event-based programming model makes it an ideal solution for taming the concurrency tiger, offering a poll-based solution in lieu of a thread-based approach (such as that used by the Apache Web Server) to handling concurrent requests. If you're not familiar with the differences between these two approaches, check out this excellent blog post.

But isn't comparing JavaScript to the Apache Web server rather unfair, if not downright nonsensical? As it turns out, a server-side JavaScript solution exists. It's called the V8 JavaScript Engine. V8 is an implementation of JavaScript written in C++ which allows you to write standalone JavaScript applications. A V8 library called Node.js (although referred to as simply Node) is fast becoming the toast of the party among those tasked with creating highly scalable real-time applications built-atop the Web thanks to its event-based approach to handling concurrency. Companies such as GitHub and Palm are already incorporating Node.js into their products, and a whole suite of amazing new Node-based products and games (see WordSquared for a really impressive example) are emerging seemingly by the day. Check out the list of applications posted on Node's GitHub wiki for more ideas of what's possible.

 

Installing Node

The easiest way to install Node is by cloning the project's GitHub-hosted repository:

$ git clone git://github.com/ry/node.git

Once the clone process has completed, you'll need to configure and install Node using the following command sequence:

$ ./configure
...
$ make
...
$ make install

Once finished, confirm Node has been properly installed by executing the following command from your terminal:

$ node --version
v0.3.6-pre

Building a Node-based Web Server

Most developers get started using a new programming language by writing and executing a language-specific implementation of Hello World. Node's particular implementation happens to be a simple Web server. Save the following code to a file named web.js:

 http = require('http');

var server = http.createServer(function (request, response) {
 response.writeHead(200, {"Content-type": "text/html"});
 response.end("<p>Hello, world!</p>");
});

server.listen(8000);

Once saved, start the Web server using the following command:

$ node web.js

Now test out the Web server by opening your browser and navigating to the address http://127.0.0.1:8000. You should see Hello, world! output to the browser window.


Tags: JavaScript, Concurrency, Node.js

Originally published on http://www.developer.com.

Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel