Matthew Harwood Matthew Harwood - 2 months ago 16
AngularJS Question

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

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

Answer

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]
Comments