Thursday, November 24, 2011

Nodeing for a Month

Writing code everyday in Node.js for a month

Edit: Jump to a future day (right now you are on Day 1):
http://nodeing.blogspot.com/2011/11/nodejs-day-2.html
http://nodeing.blogspot.com/2011/11/nodejs-day-3.html

To add to the Internet Soap Opera surrounding Node, I thought it might be interesting to actually implement something in Node.js and write down my experiences. So, for the next month I will write some Node.js code every day (each day making the program slightly bigger), and jot down my thoughts on the process here.

While I'm at it, I may as well chuck in MongoDB.

Where I'm coming from

During the day I work as a programmer on boring code using well tested (old) underlying technology (including very large quantities of Tcl code which surprisingly works well and a very old reliable SQL engine). Bugs are almost never found to be from a bug in the platform in my day job.

My uneducated thoughts before beginning this exercise
  • Both Node and MongoDB seem terribly error prone. If a Node instance has an uncaught exception and goes down, it is going to break things for several users I'm guessing.
  • Joins and query plans are both very useful. Explicit joins are going to be tedious, but at least the programmer will have a good idea of exactly what will happen on a join.
  • Error handling code full of anonymous function callbacks is going to be interesting (by which I mean horrific)
I have no doubt an excellent programmer can write a reliable system once the tools are well understood.
Good programming style might overcome some of these issues.

Getting Started

Npm (Node Package Manager) is definitely not ready for use on non-Cygwin Windows. The Node.js MSI installer works fine for Node, but npm is not able to install anything successfully - ryppi.py works fine though (thanks Jeroen Janssen!), so I'm off. I've installed express because that seems to be the thing to do.

Writing my first Node.js code

misc.js - Starting off with some boiler-place for binding my URLs - I really like the simple module system.

// Iterate through each module, bind its 'urls' assoc array.


bindurls = function(app, modules) {
for(var i=0; i<modules.length; i++) {
if(modules[i].urls) {
for(var key in modules[i].urls) {
app.get('/' + key, modules[i].urls[key]);
}
}
}
};
exports.bindurls = bindurls;

editor.js - A stub for testing out the binding

create = function(request, response) {
response.send('Hello from editor.create');
};


exports.urls = {'create': create};

init.js - A clean entry point

var express = require('express');
var editor = require('editor');
var misc = require('misc');


var app = express.createServer(express.logger());


misc.bindurls(app, [editor]);


app.get('/', function (request, response) {
response.send("<a href='/create'>Create New Thing</a>");
});


app.listen(80, function() {
console.log('Listening!');
});

First Thoughts


So far it feels good - I can understand the attraction of writing server-side code in Javascript! The code up to this point just had one bug, and the stack trace was rather odd - it will be interesting to this copes when the program starts to get some complexity.

Tomorrow


Bringing in MongoDB, and sketching out what the program will actually do.

3 comments:

  1. joins? Are you nuts? No seriously if you have document based database where your data comes nicely structured in an object you don't do joins which are very limited in my eyes.

    And MongoDB is not error prone, haven't lost any data since I started using it two years ago. The company behind it is very active in the open source field and competent in working with their users to improve mongodb's feature set.

    Btw, you might want to give coffeescript a try, it makes javascript highly readable.

    ReplyDelete
  2. Interesting that you're starting this at this time. Im doing Node.js every day for the next month too. I have a mentor taking me through the process.

    Just thought i'd point out. NPM works beautifully on Windows. I've had no issues with installs at all (although man pages fail).

    Ive used it for installs about 6 or 7 times in 3 days now and no issues at all.

    I'll also let you know how i get on ;-)

    ReplyDelete