i5ting / koa-generator

Koa' application generator for 1.x and 2.x( Express-style and support all middlewares include async/await )
https://github.com/17koa/koa-generator
MIT License
972 stars 110 forks source link

Extending default routes don't work #27

Closed legendtang closed 7 years ago

legendtang commented 8 years ago
router.get('/', async function(ctx, next) {
    ctx.state = {
        title: 'koa2 title'
    };

    await ctx.render('index', {});
})

router.get('/test', async function(ctx, next) {
    let data = {a:1, b:2}
    ctx.status = 200;
    ctx.body = data;
    await next();
});

// -> /test - 404

Do I made any mistakes? Thanks.

sinalvee commented 8 years ago

Because there is router.use('/', index.routes(), index.allowedMethods()); in app.js. When you write router.get('/test', ...), the router becomes //test, so /test got 404. You can change to router.get('test', ...).

legendtang commented 8 years ago

@sinalvee Yes, I found out that Koa routes behave differently comparing to Express. I think the generated code is quite misleading. Root route is actually // and user route is actually /user/, which coincidentally behaves the same as / and /user. Your generator does confuse many newbies like me and even a Koa user.

So, would you like to improve your example code or just add some comment on it?