Egg: post请求不生效,总是404

Created on 19 Dec 2017  ·  9Comments  ·  Source: eggjs/egg

  • Node Version: 8.9.0
  • Egg Version: 2.0.0
  • Plugin Name: egg-mysql, egg-view-nunjucks
  • Plugin Version: 3.0.0, 2.1.4
  • Platform: macOs

没有装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;
}
need-more-information not follow template

Most helpful comment

这就是为什么要求提供可复现代码库。。。

All 9 comments

请提供可复现 demo

没装egg-rest的情况下app.post('...','...')这种写法是否可行呢,是的话那egg-rest的意义是?

请提供可复现 demo

@popomore @atian25 麻烦看看有什么问题~

请提供可复现 demo

@hudawen 用 egg-init 提供最小可复现代码库

要不要加到 faq 里

在抽离出demo时发现犯了个低级错误。。
从mock切换到服务端接口时没写 'POST /api/*': 'http://localhost:7001'

这就是为什么要求提供可复现代码库。。。

Was this page helpful?
0 / 5 - 0 ratings

Related issues

occultskyrong picture occultskyrong  ·  35Comments

andylei18 picture andylei18  ·  35Comments

popomore picture popomore  ·  53Comments

zlab picture zlab  ·  55Comments

zrxisme picture zrxisme  ·  44Comments