Bird
Raised Fist0
Expressframework~20 mins

Route parameters in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Route Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when accessing /user/42?
Consider this Express route:
app.get('/user/:id', (req, res) => { res.send(`User ID is ${req.params.id}`); });

What will the server respond with when a client requests /user/42?
Express
app.get('/user/:id', (req, res) => { res.send(`User ID is ${req.params.id}`); });
AUser ID is 42
BError: Cannot read property 'id' of undefined
CUser ID is undefined
DUser ID is :id
Attempts:
2 left
💡 Hint
Look at how route parameters are accessed via req.params.
📝 Syntax
intermediate
2:00remaining
Which route definition correctly captures a product ID?
You want to define a route in Express that captures a product ID from the URL like /product/123. Which of these route definitions is correct?
Aapp.get('/product/:productId?', (req, res) => { ... });
Bapp.get('/product/productId', (req, res) => { ... });
Capp.get('/product/:productId', (req, res) => { ... });
Dapp.get('/product/:productId*', (req, res) => { ... });
Attempts:
2 left
💡 Hint
Route parameters start with a colon and match one segment.
🔧 Debug
advanced
2:00remaining
Why does this route not capture the ID parameter?
Given this Express route:
app.get('/order/:id', (req, res) => { res.send(`Order ID: ${req.params.orderId}`); });

What will happen when a client requests /order/99?
Express
app.get('/order/:id', (req, res) => { res.send(`Order ID: ${req.params.orderId}`); });
AOrder ID: undefined
BOrder ID: 99
CError: Cannot read property 'orderId' of undefined
D404 Not Found
Attempts:
2 left
💡 Hint
Check the name used in the route parameter and the name used in req.params.
❓ state_output
advanced
2:00remaining
What is the value of req.params after requesting /blog/2023/07?
Consider this Express route:
app.get('/blog/:year/:month', (req, res) => { res.json(req.params); });

What JSON will the server send when a client requests /blog/2023/07?
Express
app.get('/blog/:year/:month', (req, res) => { res.json(req.params); });
A{}
B{"year":"2023","month":"07"}
C{"month":"07"}
D{"year":"2023/07"}
Attempts:
2 left
💡 Hint
Each colon-prefixed segment captures one part of the URL path.
🧠 Conceptual
expert
3:00remaining
Which option correctly explains route parameter behavior with optional segments?
Given this route:
app.get('/archive/:year/:month?', (req, res) => { res.send(req.params); });

Which statement is true about accessing /archive/2022 and /archive/2022/12?
AAccessing /archive/2022 sets req.params to { year: '2022', month: null } and /archive/2022/12 sets req.params to { year: '2022', month: '12' }
BAccessing /archive/2022 causes a 404 error because :month is missing
CAccessing /archive/2022 sets req.params to { year: '2022', month: undefined } and /archive/2022/12 sets req.params to { year: '2022', month: '12' }
DAccessing /archive/2022 sets req.params to { year: '2022' } and /archive/2022/12 sets req.params to { year: '2022', month: '12' }
Attempts:
2 left
💡 Hint
The question mark after :month makes it optional.

Practice

(1/5)
1. What does req.params represent in an Express route handler?
easy
A. An object containing route parameter values from the URL
B. A function to send responses to the client
C. A middleware to parse JSON bodies
D. A method to define static file paths

Solution

  1. Step 1: Understand route parameters

    Route parameters are parts of the URL defined with a colon, like /user/:id.
  2. Step 2: Accessing parameters in Express

    Express stores these dynamic values in req.params as an object with keys matching parameter names.
  3. Final Answer:

    An object containing route parameter values from the URL -> Option A
  4. Quick Check:

    req.params = route parameters object [OK]
Hint: Remember: req.params holds URL dynamic parts [OK]
Common Mistakes:
  • Confusing req.params with req.body
  • Thinking req.params is a function
  • Using req.query instead of req.params
2. Which of the following is the correct way to define a route with a parameter named postId in Express?
easy
A. app.get('/post/:postId', (req, res) => { ... })
B. app.get('/post/postId', (req, res) => { ... })
C. app.get('/post/?postId', (req, res) => { ... })
D. app.get('/post/*postId', (req, res) => { ... })

Solution

  1. Step 1: Syntax for route parameters

    Express uses a colon : before the parameter name to mark it as dynamic.
  2. Step 2: Check each option

    app.get('/post/:postId', (req, res) => { ... }) uses /:postId correctly. Options A, B, and D use incorrect syntax for parameters.
  3. Final Answer:

    app.get('/post/:postId', (req, res) => { ... }) -> Option A
  4. Quick Check:

    Colon before param name = correct route param syntax [OK]
Hint: Use colon before param name in route path [OK]
Common Mistakes:
  • Omitting the colon before parameter name
  • Using query syntax instead of route params
  • Using wildcard * incorrectly for parameters
3. What will be the output of the following Express route handler when a GET request is made to /user/42?
app.get('/user/:id', (req, res) => {
  res.send(`User ID is ${req.params.id}`);
});
medium
A. User ID is undefined
B. User ID is 42
C. User ID is :id
D. Error: Cannot read property 'id' of undefined

Solution

  1. Step 1: Identify the route parameter

    The route defines a parameter :id, so req.params.id will hold the value from the URL.
  2. Step 2: Check the request URL and response

    The URL /user/42 sets id to '42', so the response sends 'User ID is 42'.
  3. Final Answer:

    User ID is 42 -> Option B
  4. Quick Check:

    req.params.id = '42' from URL [OK]
Hint: Route param value replaces :id in req.params [OK]
Common Mistakes:
  • Expecting req.params to be undefined
  • Using req.query instead of req.params
  • Forgetting to use backticks for template literals
4. Consider this Express route:
app.get('/product/:productId', (req, res) => {
  const id = req.params.product;
  res.send(`Product ID: ${id}`);
});
What is the problem with this code?
medium
A. req.params is not accessible in GET routes
B. The route path is missing a colon before productId
C. res.send cannot send strings
D. The route parameter name and req.params key do not match

Solution

  1. Step 1: Check route parameter name

    The route defines parameter productId, so req.params will have a key named productId.
  2. Step 2: Check usage of req.params

    The code uses req.params.product, which does not exist, so id will be undefined.
  3. Final Answer:

    The route parameter name and req.params key do not match -> Option D
  4. Quick Check:

    Param name mismatch causes undefined value [OK]
Hint: Match param name exactly in req.params key [OK]
Common Mistakes:
  • Using wrong key name from req.params
  • Forgetting colon in route path
  • Assuming req.params is always empty
5. You want to create an Express route that handles URLs like /order/123/item/456 to get both orderId and itemId. Which route definition and parameter access is correct?
hard
A. app.get('/order/:order/item/:item', (req, res) => { res.send(`Order: ${req.params.orderId}, Item: ${req.params.itemId}`); })
B. app.get('/order/:orderId/item/:itemId', (req, res) => { res.send(`Order: ${req.params.item}, Item: ${req.params.order}`); })
C. app.get('/order/:orderId/item/:itemId', (req, res) => { res.send(`Order: ${req.params.orderId}, Item: ${req.params.itemId}`); })
D. app.get('/order/:orderId/item/:itemId', (req, res) => { res.send(`Order: ${req.query.orderId}, Item: ${req.query.itemId}`); })

Solution

  1. Step 1: Define route with two parameters

    The route must have :orderId and :itemId to capture both values from the URL.
  2. Step 2: Access parameters correctly

    Use req.params.orderId and req.params.itemId to get the values inside the handler.
  3. Final Answer:

    app.get('/order/:orderId/item/:itemId', (req, res) => { res.send(`Order: ${req.params.orderId}, Item: ${req.params.itemId}`); }) -> Option C
  4. Quick Check:

    Route params and req.params keys must match [OK]
Hint: Use matching param names in route and req.params [OK]
Common Mistakes:
  • Mixing req.params with req.query
  • Using wrong parameter names in handler
  • Defining route without all needed parameters
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