Tiny client-side router (~900 bytes)
Tiny ~900 byte Express-inspired client-side router.
Based upon page.js and simplified to only contain manually invoked routers with callbacks. Simplified in order to work better with DerbyJS - but could also be used in other cases when the automatic routing is not desired.
page'/' indexpage'/user/:user' showpage'/user/:user/edit' editpage'/user/:user/album' albumpage'/user/:user/album/sort' sort
Defines a route mapping
path to the given
page'/' userlistpage'/user/:id' userload usershowpage'/user/:id/edit' userload usereditpage'*' notfound
Links that are not of the same origin are disregarded and will not be dispatched.
Process routes based on
Get or set the base
path. For example if page.js
is operating within "/blog/*" set the base path to "/blog".
The router uses the same string-to-regexp conversion that Express does, so things like ":id", ":id?", and "*" work as you might expect.
Another aspect that is much like Express is the ability to pass multiple callbacks. You can use this to your advantage to flatten nested callbacks, or simply to abstract components.
For example suppose you had a route to edit users, and a route to view users. In both cases you need to load the user. One way to achieve this is with several callbacks as shown here:
page'/user/:user' load showpage'/user/:user/edit' load edit
* character we could alter this to match all
routes prefixed with "/user" to achieve the same result:
page'/user/*' loadpage'/user/:user' showpage'/user/:user/edit' edit
By default when a route is not matched, lightpage.js will not do anything
Compared to page.js, and Express.js, instead of passing around a "Context" object, only the params are passed to the callbacks, along with the next callback. This is based upon the pattern of routes in DerbyJS.
One can for example access params passed in the
path, for example by accessingthe ":id" passed.
You can do this with
params.NAME much like
var id = paramsid
NOTE: Compared to page.js, there are no states that are being passed along. This is due to DerbyJS having excluded this - it's simply easier to access other DerbyJS-specifc state variables instead.
Here are some examples of what's possible
with the string to
Match an explicit path:
Match with required parameter accessed via
Match with several params, for example
Match with one optional and one required, now
will match the same route as
Use the wildcard char "" to match across segments,
ctx.params[N] where N is the
index of "" since you may use several. For example
the following will match
and so on.
Named wildcard accessed, for example
And of course
RegExp literals, where the capture
groups are available via
ctx.params[N] where N
is the index of the capture group.
$ make test$ open
(The MIT License)
Copyright (c) 2012 CJ Blomqvist <firstname.lastname@example.org>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.