一、路由的几种写法
见:https://eggjs.org/zh-cn/basics/router.html
二、路由重定向
路由内部重定向:
module.exports = app => { app.router.get(‘index‘, ‘/home/index‘, app.controller.home.index);
app.router.redirect(‘/‘, ‘/home/index‘, 302); };
路由外部重定向
// app/router.js module.exports = app => { app.router.get(‘/search‘, app.controller.search.index); }; // app/controller/search.js exports.index = async ctx => { const type = ctx.query.type; const q = ctx.query.q || ‘nodejs‘; if (type === ‘bing‘) { ctx.redirect(`http://cn.bing.com/search?q=${q}`); } else { ctx.redirect(`https://www.google.co.kr/search?q=${q}`); } }; // curl http://localhost:7001/search?type=bing&q=node.js // curl http://localhost:7001/search?q=node.js
三、路由分组(路由映射)
// app/router.js module.exports = app => { require(‘./router/news‘)(app); require(‘./router/admin‘)(app); }; // app/router/news.js module.exports = app => { app.router.get(‘/news/list‘, app.controller.news.list); app.router.get(‘/news/detail‘, app.controller.news.detail); }; // app/router/admin.js module.exports = app => { app.router.get(‘/admin/user‘, app.controller.admin.user); app.router.get(‘/admin/log‘, app.controller.admin.log); };