October 13, 2010

The Long and Winding Node

Posted in Uncategorized at 5:14 pm by dgcombs

At long last, my little NodeJS application is ready for production. What this means is that it must move out of my comfy home directory to a more standard and austere location. I chose /usr/local/bin for its new home. It must also automatically start when the system boots and stop when the system shuts down.

I was lucid enough to run Ubuntu 10.04 on my server. This is a long term support release and should be good to go for the next five years. Although the maverick in me really wanted to use the latest release, Ubuntu 10.10.

The location of my application code in the /usr/local/bin directory with supporting modules in the /usr/local/lib directory caused a few permission issues. I’d seen at least one article suggesting that the user take ownership of the full /usr/local tree. That didn’t make a lot of sense to me. Surely other services used things here. I located all the NodeJS specific directories and set their group ownership to www-data. This group is used by the user www-data which owns the /var/www website directory. By relying on group permissions for managing the NodeJS data, application and modules, I do not need to use root permissions to update or run the application. This resolves a potential security risk.

dgcombs@pie-dash:/usr/local/lib$ ls -l
drwxrwx— 16 root www-data 4096 2010-10-13 13:47 node

There are several good articles on using Upstart, the event-based program which starts and stops background tasks and services on a Linux machine. I read through one by Caolan McMahon and one by Tim Caswell in preparation for building an Upstart script for my application. It turns out that holding my breath and clenching my teeth weren’t required. The simple script below took care of it.

description “Security Metrics Node.js”
author      “Dan McGinn-Combs”
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
# We found $HOME is needed. Without it, we ran into problems
export HOME=”/var/www”
exec sudo -u www-data /usr/local/bin/SecMet/app5.js 2>&1 >> /var/log/node.log
end script

The line beginning “start on” should really say “start on startup.” But apparently, Ubuntu’s implementation doesn’t wait until all the filesystems are mounted and the network is ready before executing the Upstart files.

The program structure is relatively straightforward. I used Express and its middleware, Connect, to simplify the application development side. The calls to the MongoDB database are handled in parallel to minimize the amount of time spent waiting for data. This combination is the direct replacement for the JSP version I wrote several months ago. It is faster and leaner. Each link is associated with a get parameter and a callback function.

app.get(‘/’,function (req, res){

Since it’s an anonymous function passed as a callback, the handling of each page, including the index page can be treated like its own little program. This, of course, leads to three observations:

The Good

It keeps all the local variables in their own scope so I can reuse names with total impunity.

The Bad

It forces me to rewrite portions of the program that could be extracted and called as external functions.

The Ugly

I had to resort to using a count down variable so I could tell when the requests to MongoDB were done and the Graph URL could be generated.

Of course, for the last point, I could have employed something like Step of the Conductor. But I figured it was just too much overhead for such a simple set of transactions.

Now that this first NodeJS program is out on its own, I can concentrate on another topic that has been bugging me lately, how much log data do I really need to keep?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: