Egg.jsルーティングのいくつかの書き方、ルーティングリダイレクト、ルーティングパケット(ルーティングマッピング)

3741 ワード

一、ルートのいくつかの書き方
参照: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);     
};