哪个更正确,为什么?这取决于场景吗?有标准吗?
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/