如何创建具有多个参数的快速更新路由

How to create an express update route with multiple parameters


我想使用 fetch PUT 更新 JSON 对象中的属性。我创建了一个接受 2 URL 参数的 put 函数
app.put('/trustRoutes/:id/:item', (req, res){

我可以使用单个参数更新数据,但由于我只想更改该对象内的一个值,调用 put 将用我的新主体替换整个对象。
下面是我试过的。

app.put('/trustRoutes/:id/:item', (req, res) => {
    readFile(data => {
 
      const userId = req.params['id/item'];
      // have also tried const userId = req.params.id.item
      data[userId] = req.body;
  
//write data back to file 

我查看了其他示例,但找不到任何更新数据而不是 GET 的示例。如果有遗漏的,请告诉我。

PUT 请求非常适合完全覆盖资源,并且是幂等的。 answer 很好地解释了幂等性。对于部分更新资源,PATCH 请求是更好的选择。

app.patch('/trustRoutes/:id/:item', (req, res) => {
    readFile(data => {
        data[userId] = req.params[id];
        data[item] = req.params[item];
        // A get request for this resource would now show both of the updated values
        // Write file