0
0
Expressframework~20 mins

PUT and PATCH route handling in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PUT and PATCH Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Difference in behavior between PUT and PATCH routes

Consider an Express app with two routes: one handling PUT /user/:id and another handling PATCH /user/:id. Both update user data in a database.

What is the main difference in how these routes should behave when updating a user?

ABoth PUT and PATCH always update only the specified fields.
BPATCH replaces the entire user object, while PUT updates only the specified fields.
CPUT replaces the entire user object, while PATCH updates only the specified fields.
DBoth PUT and PATCH always replace the entire user object.
Attempts:
2 left
💡 Hint

Think about full replacement versus partial update.

📝 Syntax
intermediate
2:00remaining
Correct syntax for defining a PATCH route in Express

Which of the following code snippets correctly defines a PATCH route for updating a user's email in Express?

Express
const express = require('express');
const app = express();
app.use(express.json());
Aapp.PATCH('/user/:id', (req, res) => { /* update email */ });
Bapp.patch('/user/:id', (req, res) => { /* update email */ });
Capp.patch('/user/:id', function updateEmail(req, res) { /* update email */ });
Dapp.patch('/user/:id', async (req, res) => { /* update email */ });
Attempts:
2 left
💡 Hint

Check method name casing and syntax.

🔧 Debug
advanced
2:00remaining
Why does this PUT route not update the user correctly?

Given this Express PUT route code, why does the user data not update as expected?

Express
app.put('/user/:id', (req, res) => {
  const id = req.params.id;
  const newData = req.body;
  const user = users.find(u => u.id === id);
  Object.assign(user, newData);
  res.send(user);
});
AThe <code>id</code> from <code>req.params</code> is a string, but user IDs are numbers, so <code>find</code> returns undefined.
BThe route should use <code>PATCH</code> instead of <code>PUT</code> to update user data.
CThe <code>Object.assign</code> method does not update objects in JavaScript.
DThe <code>res.send</code> method is missing a status code.
Attempts:
2 left
💡 Hint

Check data types when searching in arrays.

state_output
advanced
2:00remaining
Resulting user object after PATCH request

Assume the user object before the PATCH request is:

{"id":1,"name":"Alice","email":"alice@example.com","age":30}

The PATCH request body is:

{"email":"alice.new@example.com","age":31}

What will the user object look like after the PATCH route updates it with Object.assign(user, req.body)?

A{"id":1,"name":"Alice","email":"alice@example.com","age":30}
B{"email":"alice.new@example.com","age":31}
C{"id":1,"name":"Alice"}
D{"id":1,"name":"Alice","email":"alice.new@example.com","age":31}
Attempts:
2 left
💡 Hint

Think about how Object.assign merges objects.

🧠 Conceptual
expert
2:00remaining
Why prefer PATCH over PUT for partial updates in REST APIs?

In REST API design, why is it generally better to use PATCH instead of PUT when updating only some fields of a resource?

APATCH allows partial updates without sending the entire resource, reducing bandwidth and risk of overwriting data.
BPUT and PATCH have identical behavior, so it does not matter which one is used.
CPATCH requires the client to send the full resource, which ensures data consistency.
DPUT is faster than PATCH because it replaces the whole resource at once.
Attempts:
2 left
💡 Hint

Consider network efficiency and data safety.