Barista is a simple URL router for NodeJS.
In a nutshell
router router// -> { controller:'beverage', action:'byLocation', beverage:'coffee', location:90210 } router// -> '/coffee/near/90210.json'
Getting Barista
Install via npm, thusly:
npm install barista
Using Barista
var Router = Router; var router = ;
Adding routes
A simple example
router
Rails-esque variable names
router router router
Globs (they also capture slashes)
router router// -> { controller:'timezones', action:'select', tzname:'America/Toronto' } router // a "catch-all" route: router// -> { controller:'errors', action:'notFound', path:'somewhere/that/four-oh-fours', format:'json' }
Match conditions
router router
Convenience methods
router router router router routeroptions '/products'
REST Resources
router
is equivalent to:
router router router router router router router
Removing Routes
In some cases, you will need to remove routes on a running router. The router.remove( name )
method will work for this, but requires
use of the otherwise unused route.name( name )
method.
Adding a name (currently only used with this functionality)
router name'products_show'
Removing a named route
router
Resolution & dispatching
The router.first( url, method [, callback] )
method can be used in two ways:
var params = router
OR
router
You can get all the matching routes like so:
var params = routerall '/products/15' 'GET' //=> [params, params, params....]
Route generation
Pass in a params hash, get back a tasty string:
router//=> '/products/5' router//=> '/products/5.json'
Set the optional second parameter to true
if you want
extra params appended as a query string:
router//=> '/products/5.json?love=cheese'
TODOs
- Add namespace support
- Better support for named routes
- Customizable resources
Things I forgot...
...might be in the /docs
folder...
...or might not exist at all.
It's broken!
Shit happens.
Write a test that fails and add it to the tests folder, then create an issue!
Patches welcome :-)
Who are you?
I'm Kieran Huggins in Toronto, Canada.