What is the rationale behind angular-fullstack's decision to implement both put and patch requests in Express?

I recently stumbled upon an article discussing the distinctions between PUT and PATCH requests (Difference between put and patch). Though I've gained some clarity on the topic, there are still aspects that remain unclear to me.

One of my major queries pertains to why the Yeoman generator for angular fullstack utilizes both:

router.put('/:id', controller.update);
AND
router.patch('/:id', controller.update);

Can someone shed light on their inclusion in the index.js files of their server.collections?

Furthermore, I would appreciate insights into the rationale behind incorporating both types of requests. How does one determine when to use PUT over PATCH, or vice versa?

'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 №1

HTTP verbs are simply different types of requests. For more information, you can refer to this link.

PUT
When you use PUT, you are requesting that the entity be stored at the provided URI. If the URI already exists, it will be modified; if not, a new resource may be created.[15]

PATCH
This method is used to make partial modifications to a resource.[18]

Answer №2

It’s important that they are not identical!

Yet, I fail to discern any distinction between the two approaches in angular-fullstack and personally find it incorrect!

  • PUT - modify the whole entity, requiring the submission of EVERY single property; otherwise, they will be deleted.
  • PATCH - partial modification, allowing you to submit only the fields you wish to alter while retaining the others on the server.

For instance: Consider this example entity:

car: {
  color: red,
  year: 2000
}

Now envision sending a patch like this:

{color: blue}

The entity now appears as follows:

car: {
  color: blue,  // MODIFIED
  year: 2000
}

However, if you were to issue a put request, the entity should look like this:

car: {
  color: blue  // MODIFIED
  year: null   // Might be null, undefined, removed, etc...
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Upon completion of a promise in an express middleware and breaking out of a loop, a 404 error is returned

In my efforts to retrieve an array of object (car) from express using database functions in conjunction with the stolenCarDb object, everything seems to be working fine. However, when attempting the following code snippet, it results in a 404 error w ...

Instructions for converting a readonly text field into an editable one using JavaScript

I'm trying to make it so that when the button (with id name_button) is clicked, the text field (with id name_text_field) becomes enabled. However, my current code doesn't seem to be working. Here's a snippet of my HTML: <input type="tex ...

Is there a way to pass a c struct pointer into javascript using ffi?

I need help passing a pointer to a struct to a method in nodejs using ffi. I am encountering an error where the type of the JavaScript struct I created cannot be determined. How can I resolve this issue? Previously, I was able to successfully implement si ...

Steps to activate internet access on the Samsung Gear S2

When I press a toggle on a web application for the gear s2 (javascript), I send out an http request: ( function () { var led001Button = document.getElementById("Led001"), led002Button = document.getElementById("Led002"); function http ...

Create a hierarchical tree structure using a string separated by dots

I am struggling with organizing a tree structure. :( My goal is to create a tree structure based on the interface below. export type Tree = Array<TreeNode>; export interface TreeNode { label: string; type: 'folder' | 'file'; ...

TranslateY animation glitching

I need help with expanding a box to 100% height after click, then collapsing and sticking to the bottom. I created a function in Vue framework to handle the animation, but it's not working smoothly. How can I make it less buggy? Check out the demo her ...

Issues with jQuery Progress Bar Functionality

As a beginner in jQuery, I am currently working on creating an interactive progress bar using jQuery. My goal is to provide a set of checkboxes on a webpage so that when a visitor checks or unchecks a checkbox, the value displayed on the progress bar will ...

Command for controlling volume in DiscordJS

Despite my efforts, I am struggling to write a command that successfully changes the volume of the music being played. Every time the player and resource variables in the volume() function are empty while the bot is playing the song. What can I do to resol ...

Is the $slice feature integrated into the php driver?

Can I use the '$slice' feature with the update() and $push functions in MongoDB? I have already attempted this, both with and without casting to (object). $db->collection->update( array('_id' => new MongoId($id)), ...

Exploring the ng-annotate and resolve features within the ui-router framework

Having an issue with my resolve methods not receiving their injected services. Here is a snippet of my code: (function () { 'use strict'; angular.module('sapphire.sample-limits').config(routes); function routes($stateProv ...

Steps for clearing input field with type=date in Protractor:

I am currently working with protractor version 4.0.4 and I'm encountering an issue where I cannot clear a date input field. It seems like Chrome is introducing some extra controls that are causing interference. You can find further details about Chro ...

What is the most effective method for including an "x-api-key" header in a NodeJS + Express GET request?

Looking to send a get request with my api key added to the 'x-api-key' header using NodeJS + Express. Currently utilizing fetch from the "isomorphic unfetch" lib: https://github.com/developit/unfetch/tree/master/packages/isomorphic-unfetch Usin ...

Issue with Phong material not responding to area light in a three.js.obj model

Trying to incorporate both a phong material and a shader area light onto my .obj model has proven to be problematic. I attempted using an array within Meshface material to load both materials, but the model vanishes completely when both are applied simulta ...

accelerating searches for wildcard text patterns

In my Postgres database, I have a table with over 8 million rows. The column I am interested in contains short text strings, usually less than 100 characters in total length. This column is defined as 'character varying(100)' and is indexed. Howe ...

Error encountered while retrieving data from Firebase and storing it in an array within an IONIC application

I am currently working on a function that retrieves data from Firebase's real-time database and stores it in an array for mapping in React. However, I am encountering a TypeScript error that I'm having trouble resolving. The error message reads ...

PHP is unable to show items containing special characters such as double quotes and apostrophes

I am facing an issue with ordering food items. Names that do not contain apostrophes work fine, but those like Pasqua Nero D'Avola Sicilia are causing problems. I have tried replacing ' with \', but the issue persists. Here is the relev ...

Database storage of image tags on websites, similar to functionality found on social media platforms such as Facebook

Looking for an image tagging solution for websites that can save tag data directly to a database instead of a local file? I've checked out , but it doesn't support database integration in the current version. Ideally, I need a solution that is co ...

How can we efficiently determine if any of the keys in an array of objects contains a value that is present in another array of arrays object?

I am working on developing a filtering system that checks for the existence of project technologies in the arrOfObjs.name. If a match is found, then the filter will allow the project to be displayed in the DOM. This filter specifically involves using a com ...

I'm encountering an issue with fetching table data using AJAX, as I consistently receive an undefined value in Console.log every time I try to fetch req_id from the table. Can anyone help me

I am struggling to fetch the req_id from the table as it always shows undefined in Console.log(). Despite having a successful database connection and data availability in the table, I have attempted both GET and POST methods without success. My main goal i ...

What exactly is the functionality of the `require('atom')` method?

Atom provides various global APIs that can be easily accessed through require('atom') How does this mechanism function? Despite not being a declared dependency, Atom packages are able to utilize these global APIs. Additionally, is it possible to ...