The routing utlity is based on laravel's implementation. See below for usage.
var Router = require("kanji/router").Router,
router = new Router();
//or register to the GLOBAL context
var router = require("kanji/router");
requiring it directly will put router into the GLOBAL context.
// basic dispatching
router.get("foo/bar", function* (next) {
this.body = "Hello world";
yield next;
});
// with options
router.get("foo/bar", {
prefix: "api/v1"
}, function* (next) {
this.body = "Hello world";
yield next;
});
GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS are supported.
Support custom verbs by using MATCH
router.match(["CUSTOM_VERB"], function*() {});
-
prefix
- prefix the URL with the value given. -
middleware
- run an array of generator functions along with the main callback. -
domain
- match a particular request with a domain
You can group your routes by using the group
method.
router.group({prefix:"test"}, function(router) {
router.get("foo/bar", function* () {});
});
// this would respond to a request to test/foo/bar
You can invoke a callback by passing a function as the last parameter or the uses
directive.
// testing/foo.js
class foo
{
*Bar(next) {
this.body = "Hello World";
yield next;
}
}
// routes.js
router.get("foo/bar", function* Bar(next) {
this.body = "Hello World";
yield next;
});
// or
router.get("foo/bar", {namespace: "testing", uses: "foo@bar"});
// would both yield `Hello World`;