Crowbar is a flexible routing system inspired by express, and director.
var router = require("crowbar")();
function auth (location, next) {
// authenticate here
}
router.param("classroom", function (location, next) {
// load classroom
next(null, classroom);
});
router.add({
enter: auth,
"/classes/:classroom": {
"/reports": {
enter: function (location, next) {
// do stuff with route
}
}
}
});
router.redirect("/classes/classid/reports", function (err, location) {
console.log(location.get("params.classroom")); // classroom model
console.log(location.get("pathname")); // /classes/classid/reports
console.log(location.get("url")); // same as pathname, but also includes query params
});
Called when a route is entered.
router.add({
"/home": {
enter: function (location, next) {
// do stuff
next(); // continue
}
}
});
router.redirect("/home", function (err, location) {
});
You can also specify multiple enter handlers:
router.add({
"/home": {
enter: [auth, function (location, next) {
// do stuff
next(); // continue
}]
}
});
Useful if you want to teardown a route before entering another.
router.add({
"/contact": {
exit: function (location, next) {
next();
},
enter: function (location, next) {
next();
}
},
"/home": {
enter: function (location, next) {
next(); // continue
}
}
});
router.redirect("/contact");
router.redirect("/home"); // exit handler called
Just like enter handlers, you can specify multiple exit handlers
States are properties set by the router which may modify your application state. This is used specifically in mojo.js.
router.add({
"/classes/:classroom": {
states: { app: "classes" },
"/reports": {
states: { classes: "reports" }
}
}
});
router.bind("location.states", function (states) {
// { app: "classes", classes: "reports" }
});
router.redirect("/classes/classid/reports");
Just like express.js, you have the ability to create parameter loaders.
router.param("classroom", function (location, next) {
console.log("location.params.classroom"); // classid
next(null, classroomModel);
});
router.add({
"/classes/:classroom": {}
});
router.redirect("/classes/classid", function (err, location) {
console.log(location.get("params.classroom")); // classroomModel
})
router.add({
"/classes/:classroom": {
name: "classroom"
}
});
router.redirect("classroom", {
params: {
classroom: "classid"
}
}, function (err, location) {
});
Kubrick comes with an HTTP listener by default, which is loaded automatically in the browser.
pathnameOrRouteName
- pathname or route name to redirect tooptions
- route name optionsquery
- route queryparams
- route params
complete
- called when redirected
adds new routes to the router
adds plugins to the router
The current location of the router
router.bind("location", function () {
// called whenever the location changes
});
Routes property
Finds a route based on the query.
router.add({
"/home": {
name: "homeRoute"
}
});
console.log(router.routes.find({ pathname: "/home" })); // /home route
console.log(router.routes.find({ pathname: "homeRoutek " })); // /home route
query parameters on the location. Note that if the query changes, those changes will also be reflected in the HTTP url.
router.bind("location", function (err, location) {
console.log(location.get("query.hello")); // blah
location.set("query.hello", "world"); // gets reflected in the HTTP url
});
router.redirect("/home?hello=blah");
similar to location.query
. location.params
are taken from the route parameters.
pathname + query params.
router.bind("location", function (err, location) {
console.log(location.get("url")); // /home?hello=blah
});
router.redirect("/home?hello=blah");
just the pathname of the location
returns TRUE of the both locations are the same
redirects the location
basic usage:
var mojo = require("mojojs"),
app = new mojo.Application();
app.use(require("crowbar"));
app.router.add({
"/home": { }
});