javascript - 为什么 Angular-fullstack 对 Express 同时使用 put 和 patch 请求?

标签 javascript angularjs mongodb express

我找到了Difference between put and patch 。读完后我有点明白其中的区别了。还是有雾气。

我的问题是: 为什么 Yeoman 生成器:angular fullstack使用

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;

服务器 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);
    });
  });
};

最佳答案

它们只是不同的 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]

关于javascript - 为什么 Angular-fullstack 对 Express 同时使用 put 和 patch 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27959060/

相关文章:

Javascript 正则表达式匹配由非数字字符分隔的数字

mongodb - 如何为mongodb配置新端口

javascript - CSS3 - 添加动态内容触发动画

javascript - ExtJs 将不同数据从一个存储加载到多个选择

angularjs - Jasmine - TypeError : Cannot read property 'spyOn' of null

javascript - 如果 json 中的数据等于 Ionic 和 Angular 的 true,则在页面上显示一个区域

AngularJS:如何使用 id 作为 $scope 变量传递来添加/删除类?

javascript - 如何通过条件嵌套对象数组来检索文档?

c# - System.TimeoutException : A timeout occured after 30000ms selecting a server using CompositeServerSelector

javascript - 我如何将 .txt 文档转换为 JavaScript 中的数组?