Challenge - 5 Problems
Query String Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does Express parse query strings by default?
Consider an Express route handler receiving a URL with query parameters like
?name=John&age=30. What will req.query contain?Express
app.get('/user', (req, res) => {
res.json(req.query);
});Attempts:
2 left
💡 Hint
Express parses query strings into an object with string values by default.
✗ Incorrect
Express uses the querystring module by default, which parses query parameters into an object with all values as strings.
📝 Syntax
intermediate2:00remaining
Which code correctly parses nested query strings in Express?
You want to parse a query string like
?user[name]=Alice&user[age]=25 into a nested object. Which Express setup achieves this?Attempts:
2 left
💡 Hint
Look for middleware that supports nested objects in URL-encoded data.
✗ Incorrect
The express.urlencoded({ extended: true }) middleware uses the qs library which supports nested objects in query strings.
🔧 Debug
advanced2:00remaining
Why does
req.query not parse arrays as expected?Given the URL
?colors=red&colors=blue, req.query.colors is a string instead of an array. What causes this behavior?Express
app.get('/colors', (req, res) => {
res.json(req.query.colors);
});Attempts:
2 left
💡 Hint
Express default parser does not convert repeated keys into arrays.
✗ Incorrect
Express's default query parser returns the last value for repeated keys as a string. To get arrays, the query string must use bracket notation or a custom parser.
🧠 Conceptual
advanced2:00remaining
What is the effect of using a custom query parser in Express?
If you configure Express with
app.set('query parser', 'simple') or app.set('query parser', 'extended'), how does it affect req.query?Attempts:
2 left
💡 Hint
Express supports two query parsers with different capabilities.
✗ Incorrect
The 'simple' parser uses Node's querystring module which does not support nested objects. The 'extended' parser uses the qs library which supports nested objects and arrays.
❓ state_output
expert3:00remaining
What is the output of this Express route with complex query string?
Given the route below and the URL
/search?filters[price][min]=10&filters[price][max]=100&tags=red&tags=blue, what is the JSON response?Express
app.use(express.urlencoded({ extended: true }));
app.get('/search', (req, res) => {
res.json(req.query);
});Attempts:
2 left
💡 Hint
express.urlencoded({ extended: true }) parses nested objects and repeated keys as arrays.
✗ Incorrect
The extended: true option uses the qs library which parses nested objects and repeated keys into arrays. All values are strings.