koa2使用koa-router无法正常运行
发布于 8 年前 作者 JerroldLee 11971 次浏览 来自 问答

代码如下,无法正常进入路由,求指点。

index.js: import Koa from 'koa’ import router from './middleware/router’ const app = new Koa() app.use(router);

router.js: import Router from ‘koa-router’ const router = new Router() export default function (ctx, next) { console.log(‘ctx:’, ctx); console.log(‘next:’, next); router.get(’/’, function (ctx, next) { console.log(‘我是首页’); ctx.body(‘我是首页’); });

router.get('/next', function (ctx, next) {
	console.log('我是next');
	ctx.body('我是next');
});

router.routes();
router.allowedMethods();
return router;

}

回到顶部