Bird
Raised Fist0
Expressframework~20 mins

Query string parsing in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Query String Parsing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2: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);
});
A{"name": "John", "age": "30"}
B{"name": "John", "age": 30}
C["name", "John", "age", "30"]
DAn empty object {}
Attempts:
2 left
💡 Hint
Express parses query strings into an object with string values by default.
📝 Syntax
intermediate
2: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?
Aapp.use(express.urlencoded({ extended: false }));
Bapp.use(express.urlencoded({ extended: true }));
Capp.use(express.json());
Dapp.use(express.query({ extended: true }));
Attempts:
2 left
💡 Hint
Look for middleware that supports nested objects in URL-encoded data.
🔧 Debug
advanced
2: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);
});
AThe server needs to use express.json() middleware to parse arrays.
BThe URL is malformed; arrays require brackets like colors[]=red&colors[]=blue.
CExpress default query parser returns the last value as a string, not an array.
Dreq.query is always a string regardless of query parameters.
Attempts:
2 left
💡 Hint
Express default parser does not convert repeated keys into arrays.
🧠 Conceptual
advanced
2: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?
ABoth options produce identical parsing results.
B'simple' parses JSON strings; 'extended' parses XML strings.
C'simple' disables query parsing; 'extended' enables it.
D'simple' uses the built-in querystring module; 'extended' uses qs for nested parsing.
Attempts:
2 left
💡 Hint
Express supports two query parsers with different capabilities.
❓ state_output
expert
3: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);
});
A{"filters": {"price": {"min": "10", "max": "100"}}, "tags": ["red", "blue"]}
B{"filters": {"price": {"min": "10", "max": "100"}}, "tags": "blue"}
C{"filters": {"price": {"min": 10, "max": 100}}, "tags": "blue"}
D{"filters": {}, "tags": ["red", "blue"]}
Attempts:
2 left
💡 Hint
express.urlencoded({ extended: true }) parses nested objects and repeated keys as arrays.

Practice

(1/5)
1. In Express, which object lets you access the query parameters from a URL like /search?term=books?
easy
A. req.query
B. req.params
C. req.body
D. req.url

Solution

  1. Step 1: Understand Express request objects

    Express provides different objects to access parts of the request: req.params for route params, req.body for POST data, and req.query for query strings.
  2. Step 2: Identify query string access

    The query string is the part after ? in a URL. Express parses it into req.query as an object.
  3. Final Answer:

    req.query -> Option A
  4. Quick Check:

    Query string = req.query [OK]
Hint: Query strings are in req.query object in Express [OK]
Common Mistakes:
  • Confusing req.params with req.query
  • Trying to read query from req.body
  • Using req.url directly without parsing
2. Which of the following is the correct way to read a query parameter named page in an Express route handler?
easy
A. const page = req.query.page;
B. const page = req.body.page;
C. const page = req.params.page;
D. const page = req.url.page;

Solution

  1. Step 1: Recall query parameter access syntax

    Query parameters are accessed via req.query as key-value pairs.
  2. Step 2: Match correct syntax

    To get the page parameter, use req.query.page. Other options access wrong objects.
  3. Final Answer:

    const page = req.query.page; -> Option A
  4. Quick Check:

    Query param 'page' = req.query.page [OK]
Hint: Use req.query.paramName to get query parameters [OK]
Common Mistakes:
  • Using req.params for query parameters
  • Trying to read query from req.body
  • Accessing req.url.page which is undefined
3. Given this Express route handler:
app.get('/items', (req, res) => {
  const limit = Number(req.query.limit) || 10;
  res.send(`Limit is ${limit}`);
});

What will be the response if the URL is /items?limit=5?
medium
A. "Limit is 10"
B. "Limit is undefined"
C. "Limit is 5"
D. "Limit is NaN"

Solution

  1. Step 1: Convert query string value to number

    The code uses Number(req.query.limit) to convert the string '5' to number 5.
  2. Step 2: Use default if conversion fails

    If conversion results in 0 or NaN, it defaults to 10. Here, 5 is valid, so limit = 5.
  3. Final Answer:

    "Limit is 5" -> Option C
  4. Quick Check:

    Number('5') = 5, so output = "Limit is 5" [OK]
Hint: Convert query strings to number before use [OK]
Common Mistakes:
  • Not converting string to number
  • Assuming req.query.limit is a number
  • Ignoring default fallback logic
4. Consider this Express route:
app.get('/search', (req, res) => {
  const term = req.query.term.toLowerCase();
  res.send(term);
});

What error will occur if the URL is /search (no query parameter)?
medium
A. SyntaxError: Unexpected token
B. TypeError: Cannot read property 'toLowerCase' of undefined
C. ReferenceError: term is not defined
D. No error, response is empty string

Solution

  1. Step 1: Check for missing query parameter

    If term is missing, req.query.term is undefined.
  2. Step 2: Calling method on undefined causes error

    Calling toLowerCase() on undefined throws a TypeError.
  3. Final Answer:

    TypeError: Cannot read property 'toLowerCase' of undefined -> Option B
  4. Quick Check:

    Undefined.toLowerCase() throws TypeError [OK]
Hint: Check if query param exists before using methods [OK]
Common Mistakes:
  • Assuming query param always exists
  • Not handling undefined values
  • Expecting empty string instead of error
5. You want to handle a query parameter sort that can be either asc or desc. If missing or invalid, default to asc. Which code snippet correctly implements this in Express?
hard
A. const sort = req.query.sort === 'asc' ? 'asc' : req.query.sort === 'desc' ? 'desc' : 'desc';
B. const sort = req.query.sort || 'asc';
C. const sort = req.query.sort === 'asc' ? 'asc' : 'desc';
D. const sort = ['asc', 'desc'].includes(req.query.sort) ? req.query.sort : 'asc';

Solution

  1. Step 1: Validate query parameter value

    We must check if req.query.sort is either 'asc' or 'desc'. Using includes() on an array is a clean way.
  2. Step 2: Provide default if invalid or missing

    If the value is not in the allowed list, default to 'asc'. This avoids invalid values.
  3. Final Answer:

    const sort = ['asc', 'desc'].includes(req.query.sort) ? req.query.sort : 'asc'; -> Option D
  4. Quick Check:

    Check allowed values with includes() and default [OK]
Hint: Use includes() to validate query param values before use [OK]
Common Mistakes:
  • Not validating allowed values
  • Using simple || which accepts invalid strings
  • Overcomplicating with nested ternaries
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard