https://eggjs.org/zh-cn/basics/router.html
router.js
使用路由的步骤
- 配置路由
- 编写控制器
- 配置文件 ```javascript ‘use strict’;
/**
- @param {Egg.Application} app - egg application */ module.exports = app => { const { router, controller } = app; router.get(‘/‘, controller.home.index); router.get(‘/sku’, controller.sku.index); }; ```
controller
app/controller/sku.js
'use strict';
const Controller = require('egg').Controller;
class SkuController extends Controller {
async index() {
const { ctx } = this;
ctx.body = 'sku实例成功';
}
}
module.exports = SkuController;
路由参数
ctx.query
url?id=12&name=lucy
class SkuController extends Controller {
async index() {
const { ctx } = this;
ctx.body = ctx.query;
}
}
ctx.params
router.get(‘/sku/detail/:id’, controller.sku.detail)
url/list-detail/23
class SkuController extends Controller {
async index() {
const { ctx } = this;
ctx.body = ctx.params;
}
}
redirect重定向
controller/news.js
async index() {
this.ctx.status = 302;
this.ctx.redirect('/shop')
}
router.js重定向
router.redirect('/list', '/index',302);