cnpm init egg --type=simple
或者npm init egg --type=simple --registry https://registry.npm.taobao.org
然后下载包
运行 npm run dev
获取参数:
async index() {
const { ctx } = this;
// 获取路由get传值参数(路由:id)
ctx.params;
// 获取url的问号get传值参数
ctx.query;
// 响应
ctx.body = '响应';
// 状态码
ctx.status = 201;
}
路由分组
// 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);
};