0
0
Expressframework~20 mins

Validating route params and query in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Route Validator 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 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}`);
});
AInvalid user ID
BUser ID: 42, Role: admin
CInvalid role
DInternal Server Error
Attempts:
2 left
💡 Hint
Check how the route extracts and validates params and query values.
📝 Syntax
intermediate
2: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();
Aapp.get('/item/:id', (req, res) => { const id = Number(req.params.id); if (id < 0) { res.send('Invalid'); } else { res.send('Valid'); } });
Bapp.get('/item/:id', (req, res) => { const id = Number(req.params.id); if (isNaN(id)) res.send('Invalid'); else res.send('Valid'); });
Capp.get('/item/:id', (req, res) => { const id = Number(req.params.id); if isNaN(id) { res.send('Invalid'); } else { res.send('Valid'); } });
Dapp.get('/item/:id', (req, res) => { const id = Number(req.params.id); if (!id) res.send('Invalid'); else res.send('Valid'); });
Attempts:
2 left
💡 Hint
Look closely at the syntax of the if statement.
state_output
advanced
2: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}`);
});
APage query param required and must be positive
BPage number: 0
CPage number: NaN
DInternal Server Error
Attempts:
2 left
💡 Hint
Consider what happens when req.query.page is undefined.
🔧 Debug
advanced
2: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}`);
});
AThe id variable is not declared properly
BThe route path is incorrect and does not match requests
CThe res.send call is missing parentheses
Dreq.params.id is always a string, so typeof id !== 'number' is always true
Attempts:
2 left
💡 Hint
Check the type of route params in Express.
🧠 Conceptual
expert
3: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?
A
const limit = parseInt(req.query.limit) || 10;
if (limit &lt;= 0) return res.status(400).send('Limit must be positive');
B
const limit = req.query.limit ?? 10;
if (typeof limit !== 'number') return res.status(400).send('Limit must be a number');
C
const limit = Number(req.query.limit);
if (!limit) limit = 10;
if (limit &lt; 1) return res.status(400).send('Limit must be positive');
D
const limit = Number(req.query.limit) || 10;
if (limit &lt; 1) return res.status(400).send('Limit must be positive');
Attempts:
2 left
💡 Hint
Consider how to parse and default numeric query params safely.