Helma Logo
main list history

Version 2 by hannes on 26. September 2008, 22:46

28 function archive(req, res, year, date) month)

Version 1 by hannes on 26. September 2008, 22:30

1Starting today, Helma NG resolves URLs of incoming requests using a regular expression based scheme rather than by looking for functions with an <code>_action</code> suffix. The request handler looks for an array called <code>urls</code> in the <code>setup</code> module with the following format:
3 var urls = [
4    [ /^$/, 'main.index' ],
5    [ /^skins$/, 'main.skins' ],
6    [ /^mount\/point/, 'webmodule.index' ],
7    [ /^continuation/, 'main.continuation' ],
8    [ /^logging/, 'main.logging' ],
9 ];
10
11Each 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.
12
13Note that it is also possible to define the pattern as string, and the action as function object like so:
14
15 var main = loadModule('main');
16  
17 var urls = [
18    [ "^$",  main.index ]
19 ];
20
21It 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:
22
23 var urls = [
24    [ /archive\/(\d{4})\/(\d{2}$/, 'main.archive' ]
25
26The function declaration of the archive action could look like this:
27
28 function archive(req, res, year, date) {
29    ...
30 }
31
32I 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