Simple express-like router for koa and roo.
var kr = require('kr');
app.use(kr.get('/pets', authenticate, pets.list));
app.use(kr.get('/pets/:name', authenticate, pets.show));
This repo is essentially a fork of koa-route, with two API differences:
- support for route middleware
- populates
this.params
$npminstallkrContrived resource-oriented example:
varkoa=require('koa');varkr=require('kr');varapp=koa();vardb={tobi: {name: 'tobi',species: 'ferret'},loki: {name: 'loki',species: 'ferret'},jane: {name: 'jane',species: 'ferret'}};function*authenticate(next){// authenticate or redirectyieldnext;}varpets={list: function*(){varnames=Object.keys(db);this.body='pets: '+names.join(', ');},show: function*(){varname=this.params.name;varpet=db[name];if(!pet)returnthis.throw('cannot find that pet',404);this.body=pet.name+' is a '+pet.species;}};app.use(kr.get('/pets',authenticate,pets.list));app.use(kr.get('/pets/:name',authenticate,pets.show));app.listen(3000);console.log('listening on port 3000');MIT