koa-route Unable to run

150 Views Asked by At

Why below code output as 'one', not 'one' 'two'? but using express-route is ok

app.use(route.get('/admin',requiredUser,index));

function *requiredUser(next){
        console.log("one"); //required session
        yield  next;
}

function *index(next) {
        console.log("two"); //ok save login
        this.body = yield render('admin');
 }
1

There are 1 best solutions below

0
On

koa-route only takes one handler - whatever you give its 2nd argument. That's why it's only executing your first handler.

You can use https://github.com/koajs/compose to combine an array of handlers into one:

var compose = require('koa-compose');

app.use(route.get('/', compose([requiredUser, index])));

Or you can use another routing library like https://github.com/alexmingoia/koa-router which has the behavior that you originally expected from koa-route:

var app = require('koa')();
var router = require('koa-router')();

router.get('/', requiredUser, index);

app.use(router.routes());
app.listen(3000);