Helma Logo
main list history

New Regexp based URL dispatching

Starting today, Helma NG resolves URLs of incoming requests using a regular expression based scheme rather than by looking for functions with an _action suffix. The request handler looks for an array called urls in the setup module with the following format:

 var urls = [
    [ /^$/, 'main.index' ],
    [ /^skins$/, 'main.skins' ],
    [ /^mount\/point/, 'webmodule.index' ],
    [ /^continuation/, 'main.continuation' ],
    [ /^logging/, 'main.logging' ], 
 ];

Each line contains a sub-array containing a pattern-action pair consisting of a regular expression and a module/function name. If the URI path of the incoming request matches the regular expression, the specified module is loaded and the function invoked.

Note that it is also possible to define the pattern as string, and the action as function object like so:

 var main = loadModule('main');
  
 var urls = [
    [ "^$",  main.index ]
 ];

It is possible to capture parts of the request path and pass them to the action as additional arguments. For example, the following pattern will cause the action to receive two additional arguments consisting of 4 and 2 digits, respectively, if matched:

 var urls = [
    [ /archive\/(\d{4})\/(\d{2}$/, 'main.archive' ]

The function declaration of the archive action could look like this:

 function archive(req, res, year, month) {
     ...
 }

I have plans for extending the facilities for passing additional arguments to the action, like defining static/literal arguments or packing the regexp groups into objects or arrays. Let me know what your ideas are, and watch this page for updates. -hns