I found Difference between put and patch . I kind of understand teh difference after reading. It's still hazy.
My question is: Why does Yeoman generator: angular fullstack use
router.put('/:id', controller.update);
AND
router.patch('/:id', controller.update);
In there index.js files of their server.collections?
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. Reading 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 !
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...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.