0

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);
    });
  });
};
Community
  • 1
  • 1
Armeen Moon
  • 18,061
  • 35
  • 120
  • 233

2 Answers2

1

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]
Harry Moreno
  • 10,231
  • 7
  • 64
  • 116
  • I'm curious to how patch knows how to partial modify a resource? PUT and PATCH from the front end are like flags, I assume. IfI was to guess, it's because the server technology knows how to handle those flags and act accordingly. Since I'm using express + mongo together, it's those technologies that know how to handle that PATCH flag. is that correct? – Armeen Moon Jan 15 '15 at 08:37
0

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.
  • PATCH - partial update, you may send only the fields you want to change and the others will be kept on the server.

e.g: 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...
}
Daniel Ribeiro
  • 162
  • 1
  • 9
  • Seems like patch is always more flexible. For what reason would you ever use PUT. Is PATCH more expensive (doubt it matters) but seems like there would be no reason to use PUT ever. – Armeen Moon Oct 02 '16 at 01:45
  • 1
    I agree. use PATCH or event POST instead of PUT. But... There is a put request generated by the framework and it does not respect the restfull rules. My opinion is they should remove the PUT or make it work as the RESTFUL spec. – Daniel Ribeiro Oct 02 '16 at 02:03