Bird
Raised Fist0
Expressframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between PUT and PATCH methods in Express route handling?
easy
A. PUT updates only specified fields, while PATCH replaces the entire resource.
B. PUT replaces the entire resource, while PATCH updates only specified fields.
C. PUT is used only for creating resources, PATCH for deleting.
D. PUT and PATCH behave exactly the same in Express.

Solution

  1. Step 1: Understand HTTP method semantics

    PUT is designed to replace the entire resource with the new data sent in the request.
  2. Step 2: Understand PATCH behavior

    PATCH updates only the fields specified in the request, leaving other fields unchanged.
  3. Final Answer:

    PUT replaces the entire resource, while PATCH updates only specified fields. -> Option B
  4. Quick Check:

    PUT replaces, PATCH updates partial [OK]
Hint: Remember: PUT replaces all, PATCH changes parts only [OK]
Common Mistakes:
  • Confusing PUT and PATCH behavior
  • Thinking PATCH deletes resources
  • Assuming PUT only updates parts
2. Which of the following is the correct way to define a PATCH route in Express to update a user by ID?
easy
A. app.get('/user/:id', (req, res) => { /* update logic */ });
B. app.put('/user/:id', (req, res) => { /* update logic */ });
C. app.patch('/user/:id', (req, res) => { /* update logic */ });
D. app.post('/user/:id', (req, res) => { /* update logic */ });

Solution

  1. Step 1: Identify the HTTP method for partial updates

    PATCH is used to update parts of a resource, so the route should use app.patch.
  2. Step 2: Check route syntax

    The route path includes /:id to specify which user to update, and the handler function is correctly defined.
  3. Final Answer:

    app.patch('/user/:id', (req, res) => { /* update logic */ }); -> Option C
  4. Quick Check:

    PATCH route uses app.patch() [OK]
Hint: PATCH routes use app.patch(path, handler) syntax [OK]
Common Mistakes:
  • Using app.put instead of app.patch for partial updates
  • Using app.get or app.post incorrectly
  • Missing route parameter syntax
3. Given this Express code snippet, what will be the response after a PATCH request to /product/5 with JSON body {"price": 20}?
const products = { 5: { name: "Pen", price: 10, stock: 100 } };
app.patch('/product/:id', (req, res) => {
  const id = req.params.id;
  Object.assign(products[id], req.body);
  res.json(products[id]);
});
medium
A. {"name":"Pen","price":20,"stock":100}
B. {"name":"Pen","price":10,"stock":100}
C. {"price":20}
D. Error: Cannot read property 'price' of undefined

Solution

  1. Step 1: Understand the initial product data

    Product with ID 5 has name "Pen", price 10, and stock 100.
  2. Step 2: Analyze the PATCH handler logic

    The handler uses Object.assign to update the product with fields from req.body. The JSON body contains {"price": 20}, so only the price changes.
  3. Final Answer:

    {"name":"Pen","price":20,"stock":100} -> Option A
  4. Quick Check:

    PATCH updates price only, other fields stay same [OK]
Hint: PATCH merges fields; unchanged fields remain [OK]
Common Mistakes:
  • Expecting full replacement of product data
  • Ignoring unchanged fields
  • Assuming error due to missing fields
4. What is wrong with this Express PUT route handler?
app.put('/item/:id', (req, res) => {
  const id = req.params.id;
  items[id] = req.body;
  res.send('Item updated');
});
Assuming items is an object storing item data.
medium
A. The route path is missing a trailing slash.
B. The route should use PATCH instead of PUT.
C. The response should be JSON, not plain text.
D. It does not parse JSON body, so req.body will be undefined.

Solution

  1. Step 1: Check if JSON body parsing middleware is used

    Without express.json() middleware, req.body will be undefined for JSON requests.
  2. Step 2: Identify the impact on the handler

    Assigning items[id] = req.body will set the item to undefined, causing data loss or errors.
  3. Final Answer:

    It does not parse JSON body, so req.body will be undefined. -> Option D
  4. Quick Check:

    Missing express.json() causes undefined req.body [OK]
Hint: Always use express.json() to read JSON request bodies [OK]
Common Mistakes:
  • Forgetting to add express.json() middleware
  • Confusing PUT and PATCH usage
  • Ignoring response content type
5. You want to implement a PUT route to replace a user profile but keep the id field unchanged. Which approach correctly handles this in Express?
hard
A. Overwrite the entire user object with req.body except manually set id back after assignment.
B. Use PATCH instead of PUT to update only the fields except id.
C. Reject the PUT request if id is present in req.body.
D. Delete the user first, then create a new user with req.body including id.

Solution

  1. Step 1: Understand PUT semantics

    PUT replaces the entire resource, so the user object will be overwritten by req.body.
  2. Step 2: Preserve the id field

    Since id should not change, after overwriting, manually set user.id back to the original value.
  3. Final Answer:

    Overwrite the entire user object with req.body except manually set id back after assignment. -> Option A
  4. Quick Check:

    PUT replaces all; preserve id manually [OK]
Hint: PUT replaces all; reset id manually after update [OK]
Common Mistakes:
  • Using PATCH when full replacement is needed
  • Allowing id to be overwritten unintentionally
  • Deleting and recreating user unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard