Beanpole - Routing framework
What are some features?
- Syntactic sugar (see below).
- Works with many protocols: amqp, http, websockets, etc.
- Hooking with other applications is a breeze with daisy.
- Works well with coffeescript
Projects using Beanpole
- celeri - CLI library
- bonsai - application server
- leche - Framework to build frontend / backend applications with the same code.
- daisy - Expose beanpole to: http, websockets, amqp (rabbitmq), etc.
- beandocs - Generate documentation from your beanpole route comments.
- beanprep - Scans beans in a given directory, and installs their dependencies.
- cupboard - Reverse package manager.
Beanpole ports
Overview
The basic route consists of a few parts: the type
of route, and the channel
. Here are some examples:
router.on('pull hello/:name', ...);
and
router.on('push hello/:name', ...);
Push Routes:
- Used to broadcast a message, or change (1 to many).
- Doesn't expect a response.
- Multiple listeners per route.
Pull Routes:
- Used to request data from a particular route (1 to 1).
- Expects a response.
- One listener per route.
- examples:
- request to http-exposed route
Using both push
, and pull
allows you to bind to a particular route. For example:
var _numUsers = 0; //numUser getter / setter function{ if!argumentslength return _numUsers; _numUsers = value; router;} //the request handler. This could be called in-app. It's also just as easily exposable as an API to http, websockets, etc.router; //pull num users initially, then listen for when num users changes.router; //triggers above listener;;
Okay, so you might have noticed I added something funky here: -pull
- that's a tag. Tags are structured like so:
router.on('pull -tagName hello/:route', ...);
or, you can add a value to it:
router.on('pull -method=GET hello/:route', ...);
As mentioned above, you can only have one listener per pull
route. HOWEVER, you can have multiple listeners per pull
route if you provide different tag values. For example:
router;
The above chunk of code is well suited for a REST-ful api without explicilty writing it for an http server. It can be used for any protocol. For example - say I wanted to delete a user using the code above:
router;
You might have guessed - tags can be used to filter routes. Okay, onto something a little more advanced: middleware. Here's an example:
router;
The token ->
denotes my/profile
must go through the authorize
route. Here are a few more use-cases:
router
Middleware is especially useful for a REST-ful interface:
router;
Middleware can also be specified without using the token: ->
.An example:
router;
Providing a wildcard *
tells the router that anything after the route must go through it.
Managing very long routes
You may run into a route which looks like this:
router
To fix the ugliness, breakup the route and escape any linebreaks:
router
Methods
router.on(type[,listener])
Listens to the given routes
type
- string or object. String would contain the route. Object would contain multiple routes / listenerslistener
- function listening to the route given.
router.push(route[, data][, options])
type
- the channel broadcast a message to.data
- the data to push to the given routeoptions
- options for the given routemeta
- tags to use to filter out listeners
router.pull(route[, data][, options][, callback])
same as push, but expects a response
router.channels()
returns all registered channels
router.getRoute(route)
returns route expression
request.write(chunk)
Initializes a streamed response. Great for sending files
request.end([chunk])
Ends a response
request.hasNext()
Returns TRUE if there's a listener after the current one.
request.next()
Moves onto the next route.
request.forward(channel, callback)
Forwards the current request to the given channel
request.thru(channel[ ,options])
Treats the given channel as middleware
request.data
Data is added here
One last goodie
Beanpole works well with coffeescript:
routeron # 'pull -method=GET say/hello': -> "hello world!"