rooster
A tiny tool to route HTTP requests with path parameters.
use:
var http = ;var rooster = ; // route parameters are between {curlybraces}var routes = { res; res; res; } { res; res; res; } // add routes with addRoutesrooster; // override the built-in handler for when route doesn't existrooster; var server = http; server; console;
api
rooster exposes 3 very simple methods
addRoutes(routes)
addRoutes is a configuration method for adding routes to rooster
routes
- an object of paths and handlers
e.g:
var routes = {...} { res; res; res; } rooster;
Add routes can be called multiple times and, each time, the new routes will be ADDED rather than the old ones being overwritten.
Path parameters are defined within {curly-braces} and are available on the request object through req.params
.
For example, if the route "GET /articles/{article}"
is matched with a GET request to "/articles/13"
, the request object will look like this:
{
path: "GET /articles/13",
params: {
article: "13"
},
...
}
addDefault(handler)
addRoutes is a configuration method for adding routes to rooster
handler(req, res)
- a cb for when requests are made to undefined paths
-
handler(req, res)
takes 2 arguments:req
- the HTTP request objectres
- the HTTP response object
e.g:
rooster.addDefault(function (req, res) {
res.writeHead(404);
res.write("Not found!");
res.end();
});
route(req, res)
route is the routing method that will match a request against one of the predefined routes
req
- the HTTP request object
res
- the HTTP request object
e.g:
var server = http.createServer(function (req, res) {
// route the request
rooster.route(req, res);
});
note
rooster is build ontop of overalls so check it out for more documentation.
license
MIT