简体   繁体   English

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

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

I found Difference between put and patch . 我发现put和patch之间存在差异 I kind of understand teh difference after reading. 看完书后,我有点理解差异。 It's still hazy. 仍然朦胧。

My question is: Why does Yeoman generator: angular fullstack use 我的问题是:为什么Yeoman生成器: 角度全栈使用

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

In there index.js files of their server.collections? 他们的server.collections中有index.js文件吗?

What's the purpose of having both? 两者兼有的目的是什么? Moreover, How would I use one vs the other? 此外,我将如何使用一个与另一个?

'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;

server controller 服务器控制器

// 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);
    });
  });
};

They are just different http verbs. 它们只是不同的http动词。 Reading https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods 阅读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]

They should'n be the same! 他们应该不一样!

But I see no difference on both methods on angular-fullstack and for me it is wrong ! 但是我发现在角度全堆栈上这两种方法都没有区别,对我来说这是错误的

  • PUT - update the entire entity, this means you need to send EVERY single property of the item otherwise they will be erased. PUT-更新整个实体,这意味着您需要发送商品的每个单一属性,否则它们将被删除。
  • PATCH - partial update, you may send only the fields you want to change and the others will be kept on the server. PATCH-部分更新,您可以仅发送要更改的字段,其他字段将保留在服务器上。

eg: Imagine the following entity: 例如:想象以下实体:

car: {
  color: red,
  year: 2000
}

Now imagine you send a patch like this: 现在,假设您发送了如下补丁:

{color: blue}

The entity is now like this: 现在,实体是这样的:

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

But instead if you sent a put the entity should like this: 但是,如果您发送了认沽权,则该实体应如下所示:

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