Aphsss
V2EX  ›  问与答

koa 的路由 404 问题

  •  
  •   Aphsss · Jul 31, 2016 · 1430 views
    This topic created in 3595 days ago, the information mentioned may be changed or developed.

    我使用 koa1 的生成器生成了个默认项目

    koa -e server

    然后在 app.js 路由部分只保留了

    koa.use('/api', index.routes(), index.allowedMethods());
    

    在 routes 下的 index.js 中,

    router.get('/', function *(next) {
        yield this.body={content:"cheers"};
    });
    
    router.get('/test', function *(next) {
        yield this.body={content:"test"};
    });
    

    这样,访问路由 /api/和 /api/test 是没问题的

    但是如果把 app.js 改成

    koa.use('/', index.routes(), index.allowedMethods());
    

    这样,访问路由 /和 /test 是 404 , 访问路由 //和 //test 是依旧 404

    请问,如何做才能使 /和 /test 不是 404 呢

    先谢谢大家了

    Supplement 1  ·  Aug 1, 2016
    1L 说的对,算是解决了。这个 use 和 express 还不太一样,等有空看看源码
    2 replies    2016-08-01 09:39:07 +08:00
    xxxyyy
        1
    xxxyyy  
       Jul 31, 2016 via Android   ❤️ 1
    试下不要前面的 path ,即 `koa.use(index.routes(), ...)`
    spark
        2
    spark  
       Aug 1, 2016
    楼上正解, 然后可以看下 koa-mount
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1106 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 18:27 · PVG 02:27 · LAX 11:27 · JFK 14:27
    ♥ Do have faith in what you're doing.