req.params lets you get values from the URL path in Express. It helps your app know what the user wants based on the URL.
req.params for route parameters in Express
app.get('/route/:paramName', (req, res) => {
const value = req.params.paramName;
// use value
});Use a colon (:) before the parameter name in the route path.
Access the parameter value with req.params.paramName.
app.get('/user/:id', (req, res) => { res.send(`User ID is ${req.params.id}`); });
app.get('/post/:postId/comment/:commentId', (req, res) => { res.send(`Post ${req.params.postId}, Comment ${req.params.commentId}`); });
This Express app listens on port 3000. It has a route /product/:productId that reads the productId from the URL and sends it back in the response.
import express from 'express'; const app = express(); const port = 3000; app.get('/product/:productId', (req, res) => { const productId = req.params.productId; res.send(`Product ID requested: ${productId}`); }); app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); });
Route parameters are always strings. Convert them if you need numbers.
If the URL does not match the route pattern, req.params will be empty.
You can have multiple parameters in one route, each with its own name.
req.params gets values from the URL path in Express routes.
Use a colon before the parameter name in the route path.
Access parameters with req.params.paramName inside the route handler.
Practice
req.params represent in an Express route handler?Solution
Step 1: Understand the role of
req.paramsreq.paramsholds 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 viareq.query, notreq.params. Response functions and middleware arrays are unrelated.Final Answer:
An object containing route parameters extracted from the URL path -> Option DQuick Check:
req.params = route parameters [OK]
- Confusing req.params with req.query
- Thinking req.params is a function
- Assuming req.params contains request body data
id in Express?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 CQuick Check:
Route params start with ':' [OK]
- Omitting the colon before parameter name
- Using query syntax instead of route param syntax
- Using wildcard '*' incorrectly
app.get('/product/:productId', (req, res) => { res.send(req.params.productId); });, what will be the response when a client requests /product/12345?Solution
Step 1: Understand route parameter extraction
The route defines a parameterproductIdwhich captures the part of the URL after/product/.Step 2: Check what is sent in the response
The handler sendsreq.params.productId, which will be the string '12345' from the URL.Final Answer:
The string '12345' -> Option AQuick Check:
req.params.productId = '12345' [OK]
- Expecting full URL or path in req.params
- Confusing req.params with req.query
- Assuming undefined if param not accessed correctly
app.get('/order/:orderId', (req, res) => { const id = req.params.id; res.send(id); }); What is the problem with this code?Solution
Step 1: Compare route parameter name and access key
The route defines parameterorderId, but the code accessesreq.params.id, which does not exist.Step 2: Understand the effect of this mismatch
Accessingreq.params.idreturns undefined, sores.send(id)sends 'undefined'.Final Answer:
The route parameter is named orderId but accessed as id, causing undefined -> Option BQuick Check:
Param name mismatch causes undefined [OK]
- Using wrong parameter name in req.params
- Assuming req.params works like req.body
- Ignoring case sensitivity in parameter names
category and itemId. Which code correctly defines the route and sends back a JSON object with both parameters?Solution
Step 1: Define route with two parameters
The route path/shop/:category/:itemIdcorrectly defines two route parameters.Step 2: Access parameters from req.params
BothcategoryanditemIdmust be accessed fromreq.paramsto 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 fromreq.params. Other options incorrectly usereq.queryorreq.body.Final Answer:
app.get('/shop/:category/:itemId', (req, res) => { res.json({ category: req.params.category, itemId: req.params.itemId }); }); -> Option AQuick Check:
Use req.params for route params, send JSON [OK]
- Mixing req.params with req.query or req.body
- Not defining all parameters in route path
- Sending plain text instead of JSON when needed
Ready to prove it?
Pass the test and this pattern moves unknown
