An Intro to Node.js

By Rob Gravelle

Node.js is part runtime environment and part library for building network applications using server-side JavaScript. It uses Chrome's JavaScript runtime engine to execute JS code directly without the need for the browser sandbox. You may recall an article that I wrote about Aptana's server-side JavaScript for their Jaxer server. By all accounts, it looks like that technology is now dead in the water. Does node.js have what it takes to get server-side JavaScript off the ground? To answer that question, this article will explain how to use node.js. Afterwards, you can decide for yourself whether or not it's a technology that you could see yourself embracing.

 

Advantages to Using Node.js

Node.js is supposed to be three things: easy to use, fast, and scalable. It's easy in that a little code goes a long way and uses it's own concurrency model that's a lot simpler than traditional O/S threads. Much like client-side JS libraries, node.js does provide abstraction so that a lot of the boilerplate code is handled under the covers. That's a great thing if you're a novice to intermediate developer, not so good if you're an expert and you want to get at some low level functionality. The scalability results from node.js's better memory efficiency under high-loads and non-blocking execution model. Functions in node.js almost never perform I/O directly, so processes never block. That leaves the developer free from worrying about dead-locking processes and able to focus on the code at hand. That's just another reason why beginner to intermediate programmers are able to use node.js to develop fast systems.

Here is a code snippet that illustrates how easy it is to write a simple server. It listens on port 8080 and responds to requests with a Hello message:

var http = require("http");
http.createServer(function(request, response) {  
  response.writeHead(200, {"Content-Type": "text/plain"});  
  response.write("Hello from the Node.js server!");  
  response.end();
}).listen(8080);
console.log('Server is listening to http://localhost/ on port 8080…');

I think that we can all agree that this is some pretty easy coding!

 

Running the Code

Let's say that we wanted to run the above server. Here's how to do it.

You can't run anything until you go to Node.js and download the latest version. It's quite easy to install as there is an installer for both Windows and Macintosh. There's also a compressed tar/gzip source code file.

Once you've completed the installation, you can try it out by opening a console and typing:

node -v

That will display the version of node.js that you are running.

To run the server:

  1. Save the above code in a file called server.js.
  2. Navigate to the folder to which you saved the server file.
  3. Type "node server.js" at the command prompt:
    E:\HTML Goodies\Node.js>node server.js
    Server is listening to http://localhost/ on port 8080…
    
  4. Open your favorite browser, enter "http://localhost:8080" in the address bar, and voila:

 

Modules in Node.js

As the size of our JavaScript libraries grow in size, it becomes necessary to organize them into different groups of similar functionality. In Java, for example, these are called Packages. Classes that need to reference a file in another package may do so using the import statement. Node.js implements the exact same feature, except that it organizes files by module instead of by package. Likewise, it utilizes the require() method to load the relevant file. In fact, we already used it in the first code example:

var http = require("http");

The same process works for our own modules. Here's one that gets the path of the requested resource from the request object. It uses the url module to parse the request url for the pathname and returns it to the caller:

var url = require("url");

function getPath(request) {  
  var path = url.parse(request.url).pathname;    
  return path;
}
exports.getPath = getPath;

Unlike a lot of other languages and libraries, node.js requires us to export public methods to the exports object. The last line of the code is where that happens.

Back in the server.js file, we can now import our requestInfo module just like we did with the http one. The only difference is that we have to consider the path from the current script. The "./" means that it resides in the same folder.

I also changed the content type to html so that we can print lines in separate paragraphs. Having saved the requestInfo module to a local variable, we can call the getPath() function by appending a dot to the requestInfo module, followed by the function call:

var http        = require("http");
var requestInfo = require("./requestinfo");

http.createServer(function(request, response) {  
  response.writeHead(200, {"Content-Type": "text/html"});  
  response.write("

Hello from the Node.js server!

");
  var path = requestInfo.getPath(request);
  response.write("

Request for " + path + " received.

");
  response.end();
}).listen(8080);
console.log('Server is listening to http://localhost/ on port 8080…');

 

Conclusion

I have always been a fan of using the same language on both the client and server sides. As it stands, with the array of server-side technologies available, it can be a real chore to reach a decision on which to use. Not to imply that variety is a bad thing, but there are definitely advantages to working with a familiar language. For that reason, let's hope that node.js catches on.


If you enjoyed this article, please contribute to Rob's less lucrative music career by purchasing one of Rob's cover or original songs from iTunes.com for only 0.99 cents each.

Rob Gravelle resides in Ottawa, Canada, and is the founder of GravelleConsulting.com. Rob has built systems for Intelligence-related organizations such as Canada Border Services, CSIS as well as for numerous commercial businesses. Email Rob to receive a free estimate on your software project. Should you hire Rob and his firm, you'll receive 15% off for mentioning that you heard about it here!

In his spare time, Rob has become an accomplished guitar player, and has released several CDs. His former band, Ivory Knight, was rated as one Canada's top hard rock and metal groups by Brave Words magazine (issue #92).

Rob uses and recommends MochaHost, which provides Web Hosting at $3.10 per month, 2 LifeTime Free Domains, and 6 Months Free!



Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •