📖 Scenario: You are building a simple Express server that responds to user requests for product details. Each product has a unique ID.
🎯 Goal: Create an Express route that uses req.params to capture the product ID from the URL and send it back in the response.
📋 What You'll Learn
Create an Express app instance called app
Create a route with path /product/:id
Use req.params.id to get the product ID from the URL
Send a response with the text Product ID: <id> where <id> is the captured parameter
💡 Why This Matters
🌍 Real World
Web servers often need to get information from the URL to show specific data, like product details or user profiles.
💼 Career
Understanding how to use route parameters with <code>req.params</code> is essential for backend web development with Express.
Progress0 / 4 steps
1
Set up Express app
Create a variable called express that requires the 'express' module. Then create an Express app instance called app by calling express().
Express
Hint
Use require('express') to import Express and then call it as a function to create the app.
2
Create route with parameter
Use app.get to create a route with path '/product/:id' and a callback function with parameters req and res.
Express
Hint
Use app.get with the path including :id to capture the parameter.
3
Access route parameter
Inside the route callback, create a variable called productId and set it to req.params.id to get the product ID from the URL.
Express
Hint
Use req.params.id to get the value of the :id parameter from the URL.
4
Send response with product ID
Use res.send to send the text `Product ID: ${productId}` as the response inside the route callback.
Express
Hint
Use template literals with backticks to include the productId variable in the response text.
Practice
(1/5)
1. What does req.params represent in an Express route handler?
easy
A. An object containing query string parameters from the URL
B. An array of middleware functions
C. A function to send responses to the client
D. An object containing route parameters extracted from the URL path
Solution
Step 1: Understand the role of req.params
req.params holds key-value pairs for parameters defined in the route path, extracted from the URL.
Step 2: Differentiate from other request properties
Query parameters are accessed via req.query, not req.params. Response functions and middleware arrays are unrelated.
Final Answer:
An object containing route parameters extracted from the URL path -> Option D
Quick Check:
req.params = route parameters [OK]
Hint: Remember: req.params gets values from URL path, not query [OK]
Common Mistakes:
Confusing req.params with req.query
Thinking req.params is a function
Assuming req.params contains request body data
2. Which of the following is the correct way to define a route with a parameter named id in Express?
easy
A. app.get('/user/*id', (req, res) => { ... })
B. app.get('/user/id', (req, res) => { ... })
C. app.get('/user/:id', (req, res) => { ... })
D. app.get('/user/?id', (req, res) => { ... })
Solution
Step 1: Recognize route parameter syntax
Express uses a colon : before the parameter name in the route path to define route parameters.
Step 2: Evaluate each option
app.get('/user/:id', (req, res) => { ... }) correctly uses /:id. Options A, B, and C use incorrect syntax for route parameters.
Final Answer:
app.get('/user/:id', (req, res) => { ... }) -> Option C
Quick Check:
Route params start with ':' [OK]
Hint: Use ':' before param name in route path [OK]
Common Mistakes:
Omitting the colon before parameter name
Using query syntax instead of route param syntax
Using wildcard '*' incorrectly
3. Given the route definition app.get('/product/:productId', (req, res) => { res.send(req.params.productId); });, what will be the response when a client requests /product/12345?
medium
A. The string '12345'
B. An error because productId is undefined
C. The string '/product/12345'
D. An empty response
Solution
Step 1: Understand route parameter extraction
The route defines a parameter productId which captures the part of the URL after /product/.
Step 2: Check what is sent in the response
The handler sends req.params.productId, which will be the string '12345' from the URL.
Final Answer:
The string '12345' -> Option A
Quick Check:
req.params.productId = '12345' [OK]
Hint: req.params.paramName matches URL part after ':' [OK]
Common Mistakes:
Expecting full URL or path in req.params
Confusing req.params with req.query
Assuming undefined if param not accessed correctly
4. Consider this Express route: app.get('/order/:orderId', (req, res) => { const id = req.params.id; res.send(id); }); What is the problem with this code?
medium
A. req.params cannot be used in GET routes
B. The route parameter is named orderId but accessed as id, causing undefined
C. The route path is missing a parameter
D. res.send cannot send strings
Solution
Step 1: Compare route parameter name and access key
The route defines parameter orderId, but the code accesses req.params.id, which does not exist.
Step 2: Understand the effect of this mismatch
Accessing req.params.id returns undefined, so res.send(id) sends 'undefined'.
Final Answer:
The route parameter is named orderId but accessed as id, causing undefined -> Option B
Quick Check:
Param name mismatch causes undefined [OK]
Hint: Access req.params using exact route param name [OK]
Common Mistakes:
Using wrong parameter name in req.params
Assuming req.params works like req.body
Ignoring case sensitivity in parameter names
5. You want to create an Express route that accepts two parameters: category and itemId. Which code correctly defines the route and sends back a JSON object with both parameters?
The route path /shop/:category/:itemId correctly defines two route parameters.
Step 2: Access parameters from req.params
Both category and itemId must be accessed from req.params to get values from the URL path.
Step 3: Verify response sends JSON with both parameters
app.get('/shop/:category/:itemId', (req, res) => { res.json({ category: req.params.category, itemId: req.params.itemId }); }); sends a JSON object with both parameters from req.params. Other options incorrectly use req.query or req.body.