我找到了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/