Egg.js中路由分组(路由映射)
Egg.js官网说我们并不建议把路由规则逻辑散落在多个地方,会给排查问题带来困扰。但是有时候我们路由非常多的时候我们建议放在多个文件中,这样利于管理 Egg.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);
};