RESTful API中的PUT前端

时间:2018-10-26 00:16:16

标签: node.js mongodb express edgejs

我正在使用一个使用node,express和mongo的api创建一个简单的网站,并使用一个简单的edgejs作为前端。所有路线都在邮递员中运行(GET,POST,PUT,DELETE)。但是,HTML5不支持PUT方法。这是一个例子。

Index.js

... 
app.put('/api/item/:id', function(req, res, next){
      About.findByIdAndUpdate({_id: req.params.id}, req.body)
      .then(function(){
        About.findOne({_id: req.params.id})
        .then(function(about){
          res.send(about)
        });
      }).catch(next)
    });

这在邮递员中很好用。这是我最初的想法。

edititem.edge

    ...
    <form 
     action="/api/item/{{item._id}}" 
      method="PUT"
      encType="multipart/form-data">
        <div class="form-group">
          <label for="title">Name</label>
          <input type="text" class="form-control" name="name" placeholder="Name">
        </div>
        <div class="form-group">
          <label for="description">Profession</label>
          <input type="text" class="form-control" name="profession" placeholder="Profession">
        </div>
        <div class="form-group">
          <label for="description">Description</label>
          <input type="text" class="form-control" name="description" placeholder="Description">
        </div>
        <div class="form-group">
          <label for="description">Email</label>
          <input type="text" class="form-control" name="footerEmail" placeholder="Email">
        </div>
        <div class="form-group">
          <label for="content">Footer Description</label>
          <textarea name="footerDescription" id="" class="form-control" rows="10" placeholder="Footer Description"></textarea>
        </div>
        <div class="form-group text-center">
            <button class="btn btn-primary">Edit Item</button>
          </div>
      </form>

我考虑过将功能绑定到“编辑”按钮,以便仅删除上一个项目并使用更新后的内容创建一个新项目,但这不是最佳实践,是吗?那会改变id,从我在Postman中看到的来看,正确的PUT不会改变id。我的想法是删除和替换。

那么如何允许从前端编辑该项目?

0 个答案:

没有答案
相关问题