没有装egg-rest,用app.get或app.router.resources对get请求都能生效,但post请求一直是404。
// services/api.js
export async function addTest(params) {
return request('/api/test', {
method: 'POST',
body: params,
});
}
// router.js
module.exports = app => {
app.get('/api/test', 'test.index');
app.post('/api/test', 'test.add');
}
// controller/test.js
const Controller = require('egg').Controller;
class TestController extends Controller {
async index() {
const ctx = this.ctx;
ctx.body = await this.service.test.getPage(ctx.request);
ctx.status = 200;
}
async add() {
const ctx = this.ctx;
ctx.body = await this.service.test.add(ctx.request);
ctx.status = 201;
}
}
module.exports = TestController;
// service/test.js
module.exports = app => {
class TestService extends app.Service {
async add(req, res, u, b) {
const body = (b && b.body) || req.body;
const item = await this.app.mysql.insert('test', {
...
});
...
}
}
return TestService;
}
请提供可复现 demo
没装egg-rest的情况下app.post('...','...')这种写法是否可行呢,是的话那egg-rest的意义是?
请提供可复现 demo
@popomore @atian25 麻烦看看有什么问题~
请提供可复现 demo
@hudawen 用 egg-init 提供最小可复现代码库
要不要加到 faq 里
在抽离出demo时发现犯了个低级错误。。
从mock切换到服务端接口时没写 'POST /api/*': 'http://localhost:7001'
这就是为什么要求提供可复现代码库。。。
Most helpful comment
这就是为什么要求提供可复现代码库。。。