froute provides simple, powerful and functional routing for node with expressive template matching. I wrote this in a day, that’s how simple it is (and that includes the awesome logo that I know you want on a t-shirt).
Install via npm:
Require the module in the usual way:
There are essentially 2 or 3 functions you need to use.
You register a route using the bind function:
You register routes with parameters in the following way (type and size parameters). Any parameters will then be passed to your handler function:
You unregister a route using the unbind function:
Unbound templates are returned by the unbind function.
When a url comes in, you call dispatch:
Your handler will then be called if a route template is matched. If there is no template matched, or handler function to call, froute.dispatch will return false (otherwise true).
A full example illustrating how the handler function works with params:
Behind the scenes
froute uses the “froute-picker” module for picking parameters from url templates and matching urls to the templates while assigning parameter values. This is an independent node module, full source here. It can also be used in isolation:
Picking parameters from a url template and matching a url to the template
What better way to manipulate and perform operations on collections than with functional.js. Some of the above functions for froute support currying (such as the bind function), happy days.
Issues and contributions
I’m open to pull requests as always, there are lots of advancements that can be made to the froute-picker.
An apple a day
… might keep the doctor away. With froute, you might even lower your blood pressure :)