Challenge - 5 Problems
Route Validator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when accessing /user/42 with valid params?
Consider this Express route that validates a numeric user ID from the route params and a 'role' query parameter. What will the server respond with when the URL is
/user/42?role=admin?Express
const express = require('express'); const app = express(); app.get('/user/:id', (req, res) => { const id = Number(req.params.id); const role = req.query.role; if (isNaN(id) || id <= 0) { return res.status(400).send('Invalid user ID'); } if (!role || (role !== 'admin' && role !== 'user')) { return res.status(400).send('Invalid role'); } res.send(`User ID: ${id}, Role: ${role}`); });
Attempts:
2 left
💡 Hint
Check how the route extracts and validates params and query values.
✗ Incorrect
The route converts the 'id' param to a number and checks if it's positive. It also checks if the 'role' query is either 'admin' or 'user'. Since 42 is valid and 'admin' is allowed, the response is the formatted string.
📝 Syntax
intermediate2:00remaining
Which option causes a syntax error in route param validation?
Which of the following Express route handlers contains a syntax error when trying to validate a numeric route param 'id'?
Express
const express = require('express');
const app = express();Attempts:
2 left
💡 Hint
Look closely at the syntax of the if statement.
✗ Incorrect
Option C misses parentheses around the condition in the if statement, causing a syntax error.
❓ state_output
advanced2:00remaining
What is the response when query param is missing?
Given this Express route that expects a 'page' query parameter as a positive integer, what will the server respond with when the URL is
/search (no query params)?Express
app.get('/search', (req, res) => { const page = Number(req.query.page); if (!page || page < 1) { return res.status(400).send('Page query param required and must be positive'); } res.send(`Page number: ${page}`); });
Attempts:
2 left
💡 Hint
Consider what happens when req.query.page is undefined.
✗ Incorrect
When no 'page' query param is provided, Number(undefined) is NaN, which is falsy, so the condition triggers the 400 response.
🔧 Debug
advanced2:00remaining
Why does this route always return 'Invalid ID' even for valid numeric IDs?
Examine this Express route. It should accept numeric IDs but always returns 'Invalid ID'. What is the cause?
Express
app.get('/product/:id', (req, res) => { const id = req.params.id; if (typeof id !== 'number') { return res.send('Invalid ID'); } res.send(`Product ID: ${id}`); });
Attempts:
2 left
💡 Hint
Check the type of route params in Express.
✗ Incorrect
Route params in Express are always strings. The check typeof id !== 'number' fails because id is never a number.
🧠 Conceptual
expert3:00remaining
Which validation approach correctly handles optional query params with defaults?
You want to validate an optional 'limit' query parameter that defaults to 10 if missing or invalid. Which code snippet correctly implements this in Express?
Attempts:
2 left
💡 Hint
Consider how to parse and default numeric query params safely.
✗ Incorrect
Option A uses parseInt to convert the string to a number, defaults to 10 if falsy, and checks if limit is positive. This handles missing or invalid inputs correctly.