Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending default routes don't work #27

Closed
legendtang opened this issue Jul 18, 2016 · 2 comments
Closed

Extending default routes don't work #27

legendtang opened this issue Jul 18, 2016 · 2 comments

Comments

@legendtang
Copy link

legendtang commented Jul 18, 2016

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
Copy link
Contributor

sinalvee commented Aug 4, 2016

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
Copy link
Author

legendtang commented Aug 4, 2016

@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?

i5ting added a commit that referenced this issue Mar 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants