koa 的路由 404 问题

2016-07-31 23:18:02 +08:00
 Aphsss

我使用 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 呢

先谢谢大家了

990 次点击
所在节点    问与答
2 条回复
xxxyyy
2016-07-31 23:42:09 +08:00
试下不要前面的 path ,即 `koa.use(index.routes(), ...)`
spark
2016-08-01 09:39:07 +08:00
楼上正解, 然后可以看下 koa-mount

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/296212

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX