使用Koa-router进行路由管理
- npm install --save koa-router
const Router = require('koa-router');
let router = new Router();
router.get('/', async (ctx)=>{
ctx.body = '用户操作首页'
})
路由模块化
- 在appApi下面创建需要模块化的文件
- 如:home.js、user.js
const Router = reuiqre('koa-router');
let router = new Router();
router.get('/home', async (ctx)=>{
ctx.body = 'home'
})
module.exports = router
const Koa = require('koa');
const app = new Koa()
const Router = require('koa-router');
let home = require('./appApi/.home.js');
let router = new Router();
router.use('/user', user.routes());
app.use(router.routes());
app.use(router.allowedMethods());