0
0
Expressframework~20 mins

Resource-based URL design in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Resourceful Router
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Express route handler?
Consider this Express route that handles a GET request for a resource by ID. What will the server respond with when a client requests /users/42?
Express
const express = require('express');
const app = express();

app.get('/users/:id', (req, res) => {
  res.send(`User ID is ${req.params.id}`);
});

// Assume server is listening
AUser ID is 42
BUser ID is :id
CUser ID is undefined
D404 Not Found
Attempts:
2 left
💡 Hint
Look at how Express uses :id in the route path and how req.params works.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a route to update a resource using PUT?
You want to create an Express route to update a book resource by its ID using the HTTP PUT method. Which code snippet correctly defines this route?
Aapp.delete('/books/:id', (req, res) => { res.send(`Update book ${req.params.id}`); });
Bapp.get('/books/:id', (req, res) => { res.send(`Update book ${req.params.id}`); });
Capp.post('/books/:id', (req, res) => { res.send(`Update book ${req.params.id}`); });
Dapp.put('/books/:id', (req, res) => { res.send(`Update book ${req.params.id}`); });
Attempts:
2 left
💡 Hint
Remember that PUT is the HTTP method used for updating resources.
🔧 Debug
advanced
2:30remaining
Why does this Express route not match requests as expected?
This Express app has two routes defined. Why will a GET request to /products/123 always respond with 'All products' instead of the product ID?
Express
const express = require('express');
const app = express();

app.get('/products*', (req, res) => {
  res.send('All products');
});

app.get('/products/:id', (req, res) => {
  res.send(`Product ID: ${req.params.id}`);
});
AThe server is missing middleware to parse URL parameters.
BExpress does not support route parameters with colons.
CThe order of routes matters; the first route matches '/products' and also '/products/123' because of missing strict matching.
DThe second route has a syntax error and never registers.
Attempts:
2 left
💡 Hint
Think about how Express matches routes in the order they are defined.
state_output
advanced
2:00remaining
What is the response when sending a DELETE request to this route?
Given this Express route, what will the client receive when making a DELETE request to /orders/99?
Express
const express = require('express');
const app = express();

app.delete('/orders/:orderId', (req, res) => {
  const id = req.params.orderId;
  res.status(200).json({ message: `Order ${id} deleted` });
});
AOrder 99 deleted
B{"message":"Order 99 deleted"}
C{"error":"Order not found"}
D404 Not Found
Attempts:
2 left
💡 Hint
Look at the response method and status code used.
🧠 Conceptual
expert
2:30remaining
Which URL best follows resource-based URL design principles for accessing comments of a blog post?
You have a blog post resource and each post can have multiple comments. According to resource-based URL design, which URL is the best choice to get all comments for a post with ID 7?
A/posts/7/comments
B/comments?postId=7
C/getCommentsForPost/7
D/posts/comments/7
Attempts:
2 left
💡 Hint
Resource-based URLs use nouns and hierarchical structure to represent relationships.