Bomber – node.js web framework — URL Routing
This weekend I finally got a chance to play around a little bit with some of the ideas I talked about in my last post on writing a new web framework with node.js.
Introducing Bomber
I have decided to call the framework “Bomber”, which is a variant spelling of “Bomer”, which is a combination of “Ben” and “Omer”, which is because my friend Omer has been volunteering API/design thoughts and advice.
You can checkout the source code for Bomber from GitHub if you want to see how it works or try it out. You must have node.js installed1 first.
Bomber is centered around the idea of “apps” (much like Django is), where an app
is just a way to group code together in a folder so that it is more portable/reusable. Unlike
with Django, Bomber expects certain code to live in certain places. For example,
the URL routing has to be in a file called routes.js
. I have written up a
description of what files an app can have in the docs folder of the source.
To actually run the Bomber server, cd
into the source code directory and run the
command ./bomber.js ./exampleProject
. This will start the server, using the
‘exampleProject’ app.
Right now Bomber doesn’t do much. If you are looking for something more stable may I suggest one of the more established projects?
Bomber Routing
I’ll show you how it works first, and then after, I’ll talk about why I decided to have it work the way it does.
The goal of the routing in Bomber is to determine which function should be run given a URL.
Here is an example of what a routes.js
file could look like:
All this file needs to do, is create a Router
object, add routes to it, and then
finally export that router object.
If you have used Rails this should look very familiar.
How adding the routes works is you give the router the URL to match, and then an object with the set of parameters that should be used to decide which function to run.
The app
parameter tell Bomber which app to use, view
tells it what view file to
look in, and action
tells it the name of the function to run. Any other
parameters (or those declared in the URL, like :id
) will find their way to the
function (I’m not sure how yet, wait for another post!). If you don’t specify
app
, Bomber will use the current app.
Additionally, if you want complete control over the process, you can give it a regular expression instead of a string for the URL to match. Behind the scenes all routes are converted to regular expressions, anyway.
The Thought Process
There were 3 ways I considered using when designing this: the Sinatra way, the Django way, and the Rails Way.
The Sinatra way is very popular with node.js frameworks (I have yet to find one that doesn’t do it this way). The Sinatra way is to have the routing in the same file and even inline with the code that is to be run. It looks something like this (to take an example from Express):
Or like this (to take an example from Djangode):
This is a great system, as long as a) you don’t have too many routes and b) your functions aren’t too large. Otherwise I feel like it gets a bit unwieldy trying to keep track of what happens where. I’m kind of envisioning Bomber being used for slightly larger projects. And in that case it is nice being able to see all your routes at one glance.
This means I’d like the routes to be in a separate file from the functions.
The Django way works like this. You have a file in which you declare your routes, and each route maps to a specific function else where in the app. Here’s an example from the docs:
I have two complaints about the way Django does routing.
- I have to write out a route for every different function I want to use.
- Named groups aren’t very elegant (and this is Python’s fault not Django’s).
But there are two things I really like about the way Python does routing.
- It is great being able to just write a regular expression. I like that power.
- You can pass off the processing of a URL to other routing files. Here’s the example from the documentation:
This is really great. And is something I hope to add to the routing for Bomber soon.
The Rails way works very similar to how I ended up doing routing in Bomber.
It is also very similar to how Django does its routing. There is a subtle difference, however. With Rails, instead of having a route match to a function, a route just generates an object which has all the parameters needed to find the function. Then Rails uses this object to actually do that. The difference is that you can use parameters gathered from the URL to tell Rails which (to use Rails lingo) controller to use, or which action to run. This means you can set up defaults and not have to do anything else (if you want to). Even if you add controllers or actions later, your defaults still handle everything. Here’s an example from the Rails Guide on Routing:
This solves my first complain about Django.
I also like it much better how you don’t have to think about regular expressions if you don’t want to. “Give me everything between this slash and this one and call it X” seems so intuitive to me. This solves my second complaint about Django.
In the end I am hoping to have a system that takes the best parts from Django and Rails, and I think I am on my way.
(I can’t link directly to that section, scroll down). And there is a Mac OS X installer if that is easier for you. But the installer will soon become out of date. It will probably be easiest to stay current if you checkout and build from source. Node is changing quickly these days!
-
Simon Willison has a great write up of how to get it installed ↩