[eggjs/egg]resources 路由进不去update方法

2025-11-20 153 views
0

路由注册: router.resources('express', '/api/express', controller.express); controller->express:

const Controller = require('egg').Controller;

class HomeController extends Controller {
    async index(ctx) {
        ctx.body='dasd';

    }

    async create(ctx) {
        ctx.body='dasd';
    }

    async show(ctx) {
        ctx.body='dasd';
    }

    async update(ctx) {
        ctx.body='dasd';
    }

    async destroy(ctx) {
        ctx.body='dasd';
    }
}

module.exports = HomeController;
Request URL:http://127.0.0.1:7001/api/pets/1
Request Method:POST
Status Code:404 Not Found
Remote Address:127.0.0.1:7001
Referrer Policy:no-referrer-when-downgrade

其他方法都可以,就update 404,也打印不了

其他resources试了也是一样,都行就update 404 进不去

回答

4

@egg-bot 直接用egg-init egg-example --type=simple 添加路由router.resources('pets', '/api/pets', controller.pets); 添加pets controller:

'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    this.ctx.body = '416546546';
  }
  async update(ctx) {
    ctx.body = 123456;
  }
}

module.exports = HomeController;

config.default.js 关闭csrf:

config.security= {
        csrf: {
            enable: false,
        }
    };

再使用postman post http://127.0.0.1:7001/api/pets/1 结果404

PS F:\egg-example> egg-init --version
1.15.0
1

@callmesoul

https://github.com/eggjs/egg/issues/3310

尤其是最后一项『最小可复现仓库』 ,请通过 npx egg-init --type=simple 来初始化并 上传到你的 GitHub 仓库。

1

看错了不好意思, 我记得以前好像也是post的啊

4

这个一直没改过。

5

好吧...

2

经过查看Rails的form_for文档,发现需要在form表单里,添加

<input type="hidden" name="_method" value="patch" />

原method还是直接按照method="post"设置