Challenge - 5 Problems
Query String Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What does
req.query contain?In an Express app, when a client sends a URL like
/search?term=book&sort=asc, what will req.query contain?Attempts:
2 left
💡 Hint
Think about how query strings are parsed into key-value pairs.
✗ Incorrect
The req.query object contains the parsed query string parameters as key-value pairs. For the URL /search?term=book&sort=asc, req.query will be { term: 'book', sort: 'asc' }.
📝 Syntax
intermediate1:30remaining
Accessing a query parameter safely
Given the URL
/items?category=tools, which code correctly accesses the category query parameter in Express?Express
app.get('/items', (req, res) => {
// Access category here
});Attempts:
2 left
💡 Hint
Query parameters come from the URL after the question mark.
✗ Incorrect
Query parameters are accessed via req.query. Route parameters use req.params, and POST data uses req.body.
🔧 Debug
advanced2:00remaining
Why is
req.query.page undefined?An Express route expects a query parameter
But the response shows
page. The URL is /list?page=2. The code is:app.get('/list', (req, res) => {
const page = req.query.page;
res.send(`Page number is ${page}`);
});But the response shows
Page number is undefined. What is the likely cause?Attempts:
2 left
💡 Hint
Check the URL format carefully.
✗ Incorrect
If the URL does not contain a question mark before query parameters, req.query will be empty. The URL must be /list?page=2, not /listpage=2.
❓ state_output
advanced2:00remaining
What is the output of this Express route?
Consider this Express route:
What will be the JSON response for the URL
app.get('/filter', (req, res) => {
const { type = 'all', limit = 10 } = req.query;
res.json({ type, limit });
});What will be the JSON response for the URL
/filter?limit=5?Attempts:
2 left
💡 Hint
Remember that query parameters are strings by default.
✗ Incorrect
Query parameters are strings, so limit is the string "5". The default type is used because it was not provided.
🧠 Conceptual
expert2:30remaining
Handling multiple values for the same query key
If a client sends the URL
/search?tag=js&tag=node&tag=express, what will req.query.tag contain in Express by default?Attempts:
2 left
💡 Hint
Express uses a simple parser for query strings by default.
✗ Incorrect
By default, Express's query parser returns the last value for repeated keys. So req.query.tag will be the string "express".