Fleegix.org

Geddy: Web framework for Node.js

2010-03-15 22:46:00

So I've posted previously about the embarrassment of riches that we have with Node.js modules, specifically Web frameworks.

I count no less than 14 frameworks and micro-frameworks for building Web-apps with Node right now on the list of Node modules.

I had actually hoped to use one of these to build a small Node app, but as it turns out, many of these are either half-finished, or are broken with current versions of Node. Pretty understandable, given how quickly Node is changing.

Express looks good, but I'm a little more interested in something that will let me build complex apps -- and the Sinatra-style routing, with the verb and everything kind of inline there, seems a little verbose. I prefer the simpler DSL of Rails-/Merb-style routes. To me it's just more expressive, flexible, and powerful.

So I started building a basic router, looking at a lot of the ideas in Bomber.js, and remembering back on how simple and hackable the Merb framework was to use.

The result is Geddy. It's a small Web-app development framework that's still very embryonic -- but it does routing and invoking controller actions, and serves static files. I'm having a blast hacking on it. It's crazy fun doing all this server-side code in JavaScript.

Of course, it's named in honor of Geddy Lee, the bassist for Rush.

Routing

Routes in Geddy look like this:

r = new Router();
r.match('/').to({controller: 'Main', action: 'index'}).name('main');
r.match('/users/:userid/lerxstizer/:lerxstid').to(
  {controller: 'Users', action: 'lerxstizer'});

This should look pretty familiar to anybody who's used Merb or Rails. The controller here is really just an execution context for the action, which is really just a handler method.

The advantage of thinking of these as 'controller' and 'action' is that it allows the execution context of the handler method to inherit a bunch of helpful methods from a base Controller.

It also leverages people's understanding of MVC so the on-ramp to building something is minimal.

Controllers and actions

On startup, Geddy scans the app/controllers directory for JavaScript files, and loads them as available controllers.

A sample controller file looks like this (in file app/controllers/users.js):

var Users = function () {
  this.lerxstizer = function (params) {
    // params should have userid, lerxstid, and any query-string params
    this.render(JSON.stringify(params));
  };
};

exports.Users = Users;

The controllers are all required and instantiated based on convention, but I'm not going all meshugah with the Railsy pluralization rules.

The controller files should be all lowercase names, with underscores separating words. The constructor function in the file should be a camel-cased, initial-capital-letter version of the same name.

Some examples:

File Constructor
app/controllers/snow_dogs.js SnowDogs
app/controllers/priests_of_syrinx.js PriestsOfSyrinx

The action is a method then called on the controller instance, and passed a params object that contains any params pulled off by matching the pattern in the router -- and any query-string params.

So for example, in the case of the 'lerxstizer' example above, if you hit the following URL:

/users/alex/lerxstizer/2112?code=YYZ

The params object will look like this:

{
  userid: 'alex',
  lerxstid: '2112',
  code: 'YYZ'
}

Static files

It's impossible to develop a Web application without serving the static assets needed. I took a look at how Paperboy and other frameworks did it, and added static-file support for Geddy.

In development-mode, if an URL matches no routes, Geddy will serve up matching filenames in the public/ directory rather than responding with a 404. Obviously this directory path needs to be configurable at some point.

Stuff to do

There's a long list of stuff to hack in:

  • Resource-based routes
  • Content negotiation
  • Templating support
  • Better error handling
  • Controller-generator script
  • Kiwi package

I also need to separate out the framework code from what will eventually be generated app code. Lots of stuff to work on, and hopefully lots of opportunity to get to know Node better.

About

This is the blog for Matthew Eernisse. I currently work at Yammer as a developer, working mostly with JavaScript. All opinions expressed here are my own, not my employer's.

Related

Previous posts

All previous posts ยป

This blog is a GeddyJS application.