siren-enrouten
v1.0.1
Published
A koa route initialization and configuration module.
Downloads
4
Readme
siren-enrouten
A Route configuration middleware for koa, and this is based on express-enrouten.
Usage
var koa = require('koa'),
enrouten = require('siren-enrouten');
var app = koa();
app.use(enrouten(app));
Configuration
Please refer to express-enrouten, except that all middleware should be generators.
Note
If you want to mount the enrouten using middleware like koa-mount, you SHOULD set the mountpath
for enrouten, for example:
var app = require('koa'),
mount = require('koa-mount'),
enrouten = require('siren-enrouten');
var app = koa();
app.use(mount('/foo', enrouten(app, { mountpath: '/foo' })));
Otherwise, when you use enrouten.url
, it will return an unmounted url.
routerOptions
The routerOptions
configuration option (optional) allows additional options to be
specified on each Router instance created by siren-enrouten
. Please refer to siren-router.
app.use(enrouten(app, {
directory: 'controllers',
routerOptions: {
caseSensitive: true
}
}));
Named Routes
For index
and directory
configurations there is also support for named routes.
When you use named routes by the passed siren-router instance, enrouten will fetch
the named routes out and you can get the named url by ctx.enrouten.url
or enrouten.url
.
For example:
var enrouten = require('siren-enrouten');
app.use(enrouten(app));
var enrouten = require('siren-enrouten');
module.exports = function (router) {
router.get('my-foo', '/foo/:id', function *(next) {
// ...
});
router.get('/my-foo2', '/foo2/:id', function *() {
this.redirect(this.enrouten.url('my-foo', this.params.id));
// or this.redirect(enrouten.url(this.app, 'my-foo', this.params.id));
});
};
Controller Files
Please refer to express-enrouten, except that all middleware should be generators.
Tests
$ npm test
Coverage
$ npm run-script test-cov && open coverage/lcov-report/index.html