我的koa@next应用程序有以下结构。我使用koa-router@next进行路由:
./app.js
const Koa = require('koa');
const router = require('koa-router')();
const index = require('./routes/index');
const app = new Koa();
router.use('/', index.routes(), index.allowedMethods());
app
.use(router.routes())
.use(router.allowedMethods());
module.exports = app;./路由/index.js
const router = require('koa-router')();
router.get('/', (ctx, next) => {
ctx.body = 'Frontpage';
});
router.get('/hello', (ctx, next) => {
ctx.body = 'Hello, World!';
});
module.exports = router;我在Not Found路径上得到了/hello错误。
依赖性版本:
"dependencies": {
"koa": "^2.0.0-alpha.7",
"koa-router": "^7.0.1",
},koa-router v7.1.0也是如此。
谢谢你的帮助!
发布于 2016-11-30 13:12:47
像这样重组应用程序可以解决这个问题。我想是时候从精神上抛弃快递了。
./app.js
import Koa from 'koa';
import index from './routes/index';
const app = new Koa();
app.use(index.routes(), index.allowedMethods());
export default app;./路由/index.js
import Router from 'koa-router';
const router = new Router();
//const router = new Router({ prefix: '/subroute' })
router.get('/', (ctx, next) => {
ctx.body = 'Frontpage';
});
router.get('/hello', (ctx, next) => {
ctx.body = 'Hello, World!';
});
export default router;https://stackoverflow.com/questions/40866134
复制相似问题