繁体   English   中英

为什么angular-fullstack同时使用put和patch请求进行表达?

[英]Why does angular-fullstack use both put and patch requests for express?

我发现put和patch之间存在差异 看完书后,我有点理解差异。 仍然朦胧。

我的问题是:为什么Yeoman生成器: 角度全栈使用

router.put('/:id', controller.update);
AND
router.patch('/:id', controller.update);

他们的server.collections中有index.js文件吗?

两者兼有的目的是什么? 此外,我将如何使用一个与另一个?

'use strict';

var express = require('express');
var controller = require('./thing.controller');

var router = express.Router();

router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id', controller.update);
router.patch('/:id', controller.update);
router.delete('/:id', controller.destroy);

module.exports = router;

服务器控制器

// Updates an existing thing in the DB.
exports.update = function(req, res) {
  if(req.body._id) { delete req.body._id; }
  Thing.findById(req.params.id, function (err, thing) {
    if (err) { return handleError(res, err); }
    if(!thing) { return res.send(404); }
    var updated = _.merge(thing, req.body);
    updated.save(function (err) {
      if (err) { return handleError(res, err); }
      return res.json(200, thing);
    });
  });
};

它们只是不同的http动词。 阅读https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods

PUT
Requests that the enclosed entity be stored under the supplied URI. If the URI refers
to an already existing resource, it is modified; if the URI does not point to an existing
resource, then the server can create the resource with that URI.[15]

PATCH
Applies partial modifications to a resource.[18]

他们应该不一样!

但是我发现在角度全堆栈上这两种方法都没有区别,对我来说这是错误的

  • PUT-更新整个实体,这意味着您需要发送商品的每个单一属性,否则它们将被删除。
  • PATCH-部分更新,您可以仅发送要更改的字段,其他字段将保留在服务器上。

例如:想象以下实体:

car: {
  color: red,
  year: 2000
}

现在,假设您发送了如下补丁:

{color: blue}

现在,实体是这样的:

car: {
  color: blue,  // CHANGED
  year: 2000
}

但是,如果您发送了认沽权,则该实体应如下所示:

car: {
  color: blue  // CHANGED
  year: null   // May be null, undefined, removed, etc...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM