Hello NodeJS

Here it is! My new personal website is online and ready to go!

For as long as I've done web development, my sites have been created with ASP.NET. However, this time around I decide to try something different and went with NodeJS.

JavaScript has been my favorite language for a long time now despite some of it's baffling quirks. Naturally, when evaluating a new server side language, I decided to give Node a try.

Keep in mind that I'm still quite new with Node and I've only had a few weeks of experience mostly working in my evenings. The thoughts here are mostly my initial reactions to using Node.

Callbacks Are A Problem

Initially, I read that Node was designed to use Callbacks since it avoided many of the common problems associated with multi-threaded applications. I didn't think they would be that difficult to work with, but they can and will get out control quickly if you aren't careful.

For example, if I wanted to check for some files and read the contents, I had originally expected I would write something similar to the following example.

var node = {
    path: require('path'),
    fs: require('fs')
};

// reads the contents of a file.
// checks for a default file name
function read_post(path) {

    //check for the default file
    var full_path = path + '.html';
    if (!node.path.exists(full_path)) {

        //check for the specific file
        full_path = path + '/index.html';
        if (!node.path.exists(full_path)) 
            return null;
    }

    //if there is a matchin
    return node.fs.readFile(full_path);
}

However, code normally ended up looking like the following snippet.

var node = {
    path: require('path'),
    fs: require('fs')
};

// reads the contents of a file.
// checks for a default file name
function read_post(path, with_post) {
  var default_path = path + '.html';

  //handles reading the content
  var read_file = function(file_path) {
    node.fs.readFile(file_path,
      function with_contents(err, contents) {
        var text = contents.toString();
        with_post(contents);
      });
  };

  //check for two file types
  node.path.exists(default_path,
    function if_default_path(default_exists) {

      //make sure if it exists
      if (!default_exists) {

        //if not, try the default file
        var specific_path = path + '/index.html';
        node.path.exists(specific_path,
          function if_specific_path(specific_exists) {

            //no file was found
            if (!specific_exists) return with_post();

            //specific file does exist, use it
            read_file(specific_path);

          });
      }
      //default file exists, read it
      else read_file(default_path);

    });

}

This code is not an ideal example : The point here is to illustrate how quickly you can end up with an excessive amount of callbacks for even simple tasks.

Two important things to note here.

  1. You can do things to reduce the amount of nested code which does help to limit complexity.
  2. You can download open source libraries to help avoid the callback mess. I'm pointing out Node's behavior by default.

I've long held the opinion that dynamic languages only work if you can limit complexity to a minimum. The excessive amount of callbacks found in Node does quite the opposite.

Fragile: Handle With Care

When Node crashes, it crashes hard. It is easy to take down an entire Node app with a single error floating somewhere in your code.

Dynamic languages are already prone to hard to find errors which lie dormant until code is executed. Node amplifies these problems by taking the whole site down with a single exception.

You aren't without solutions, however.

  1. Node can allow you to catch and handle global errors. Just make sure not to have any errors in your error handler.
  2. Several open source libraries exist that can monitor for when a Node site crashes and even restore the app when it goes offline.

I don't really consider this to be that much of a problem. Just consider this a warning that if your Node application crashes, you must have good error handling in place since it won't come back online on its own.

JavaScript++

I do have some complaints about Node, but I don't want that to sway anyone from trying it. It is a refreshing change of pace. It's great to be using JavaScript in more than just the browser.

There's already a great community of fellow Node developers working to create libraries to help speed up your application development.

In any case, my new site is written using Node so I anticipate I'll be using it for the forseeable future.

January 25, 2012

Hello NodeJS

New Hugoware.net website written in NodeJS and what I've learned.