router的几种写法
router.verb('path-match', app.controller.action);router.verb('router-name', 'path-match', app.controller.action);router.verb('path-match', middleware1, ..., middlewareN, app.controller.action);router.verb('router-name', 'path-match', middleware1, ..., middlewareN, app.controller.action);
router.get('/', controller.home.index); router.get('/', auth1,auth2,controller.home.index); router.get('index','/', controller.home.index); router.get('index','/', auth1,auth2,controller.home.index);
重定向
目录结构
路由内部重定向
controller/news.js
async index() { this.ctx.status = 302; this.ctx.redirect('/shop') }
路由外部重定向
router.js
router.redirect('/news', '/shop',302);
路由分级(路由映射)
目录结构
分路由
router/admin.js
module.exports = app => { const { router, controller } = app; router.get('/admin/article',controller.admin.article.index); router.get('/admin/user',controller.admin.user.index);};
router/api.js
module.exports = app => { const { router, controller } = app; router.get('/api/article', controller.api.article.index); router.get('/api/user', controller.api.user.index);};
router/index.js
module.exports = app => { const { router, controller } = app; router.get('/', controller.home.index); router.redirect('/news', '/shop',302); router.get('/shop', controller.shop.index);};
总路由
router.js
module.exports = app => { require('./router/admin')(app) require('./router/api')(app) require('./router/index')(app)};