1.GET 方法
// 商品详情
async detail() {
const { ctx } = this;
console.log(ctx.query);
ctx.body = `id==${ctx.query.id}`;
}
async detail2() {
const { ctx } = this;
console.log(ctx.params);
ctx.body = `id==${ctx.params.id}`;
}
router.get('/product/detail', controller.product.detail);
router.get('/product/detail2/:id', controller.product.detail2);
2.POST 方法
需要先关闭 csrf j检查
config/config.default.js
config.security = {
csrf: {
enable: false,
}
};
// 新增
async create() {
const { ctx } = this;
console.log(ctx.request.body);
const { name, weight } = ctx.request.body;
ctx.body = {
name,
weight
};
}
router.post('/product/create', controller.product.create);
3.PUT 方法
// 更新
async update() {
const { ctx } = this;
console.log(ctx.params);
ctx.body = {
id: ctx.params.id
}
}
router.put('/product/update/:id', controller.product.update);
4.DELETE 方法
// 删除
async delete() {
const { ctx } = this;
console.log(ctx.params);
ctx.body = {
id: ctx.params.id
}
}
router.delete('/product/delete/:id', controller.product.delete);
.