gpt4 book ai didi

node.js - 表达:req.params 与 req.body (JSON)

转载 作者:搜寻专家 更新时间:2023-10-31 23:28:49 24 4
gpt4 key购买 nike

哪个更正确,为什么?这取决于场景吗?有标准吗?

router.post("/user", function(req, res) {
let thisUserId = req.body.userId;
});

router.post("/user/:userId", function(req, res) {
let thisUserId = req.params.userId;
}

谢谢!

最佳答案

这个问题更多的是关于 RESTful API 约定,而不是 node 或 express。基于公认的REST conventions ,这是基本的 CRUD 操作:

/* fetch all users */
GET /users

/* fetch specific user */
GET /users/:userId

/* create new user */
POST /users

/* edit specific user */
PUT /users/:userId

/* delete specific user */
DELETE /users/:userId

所以在你的情况下,考虑到你想创建一个用户,我会说 req.body 更合适。

编辑:另一个支持这种情况的有用资源:10 best practices for better RESTful API .

关于node.js - 表达:req.params 与 req.body (JSON),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54903043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com