Beanpole - Routing framework
Motivation
- Abstract communication between parts of an application
- keeps code modular
- works in-app, or with other protocols: amqp, http, etc.
This:
router
Versus somethine like this:
var { ;}
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
Collect Routes:
- Used to request data from many listeners (1 to many, similar to pull).
- Expects a response.
Error Handling
{ ifcreditsuser != 'user' || creditspass != 'pass' return ; ;} router //errorvar req = router;
Custom Routes
You can easily create custom route handlers. Take celeri for example:
var beanpoll = structr = ; //handles the message, response, and middlewarevar CmdMessenger = ; //the "Event Emitter"var CmdDirector = ; var router = beanpoll; //use the new pluginrouter; //use it: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
You can also split it up:
router 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.request(router)
returns the request builder
router //called when the second param is present. //separated error from the response //called when there's a result, or error { }) //type of request: push, pull, collect, your own;
router.push(route[, query][, headers])
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[, query][, headers][, callback])
same as push, but expects a response
router.channels()
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.