0
0
Expressframework~20 mins

req.query for query strings in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Query String Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1: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?
A{"term": "book", "sort": "asc"}
B["term", "book", "sort", "asc"]
C"term=book&sort=asc"
Dundefined
Attempts:
2 left
💡 Hint
Think about how query strings are parsed into key-value pairs.
📝 Syntax
intermediate
1: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
});
Aconst category = req.params.category;
Bconst category = req.query.category;
Cconst category = req.body.category;
Dconst category = req.query[0];
Attempts:
2 left
💡 Hint
Query parameters come from the URL after the question mark.
🔧 Debug
advanced
2:00remaining
Why is req.query.page undefined?
An Express route expects a query parameter 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?
AThe client did not send the query string properly; the URL is missing the question mark.
BThe Express app is missing middleware to parse query strings.
CThe query parameter name is case-sensitive and should be uppercase.
DThe route path is incorrect and does not match the URL.
Attempts:
2 left
💡 Hint
Check the URL format carefully.
state_output
advanced
2:00remaining
What is the output of this Express route?
Consider this Express route:
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?
A{"type": "all", "limit": 5}
B{"type": undefined, "limit": 5}
C{"type": "all", "limit": "5"}
D{"type": "all", "limit": 10}
Attempts:
2 left
💡 Hint
Remember that query parameters are strings by default.
🧠 Conceptual
expert
2: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?
Aundefined
B["js", "node", "express"]
C"js"
D"express"
Attempts:
2 left
💡 Hint
Express uses a simple parser for query strings by default.