Egg.js路由的几种写法、路由重定向、路由分组(路由映射)
2021-02-06 21:16
标签:list google https query use ports style column get 一、路由的几种写法 见:https://eggjs.org/zh-cn/basics/router.html 二、路由重定向 路由内部重定向: 路由外部重定向 Egg.js路由的几种写法、路由重定向、路由分组(路由映射) 标签:list google https query use ports style column get 原文地址:https://www.cnblogs.com/loaderman/p/11555330.htmlmodule.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);
};
文章标题:Egg.js路由的几种写法、路由重定向、路由分组(路由映射)
文章链接:http://soscw.com/index.php/essay/51882.html