A query string is the part of a URL after the question mark (?) that contains key-value pairs used to send data to the server.
Click to reveal answer
beginner
How does Express.js parse query strings by default?
Express.js automatically parses query strings into an object accessible via req.query in route handlers.
Click to reveal answer
beginner
Example: What does req.query contain for URL /search?term=books&sort=asc?
{ term: 'books', sort: 'asc' } - an object with keys and values from the query string.
Click to reveal answer
beginner
How can you access a specific query parameter in Express?
Use req.query.parameterName. For example, req.query.term gets the value of the 'term' parameter.
Click to reveal answer
beginner
What happens if a query parameter is missing in the URL?
The corresponding key in req.query will be undefined or not present, so you should check before using it.
Click to reveal answer
In Express.js, where do you find parsed query string parameters?
Areq.params
Breq.body
Creq.query
Dreq.headers
✗ Incorrect
Express parses query strings into the req.query object.
What symbol starts the query string in a URL?
A?
B&
C#
D/
✗ Incorrect
The question mark (?) marks the start of the query string.
If the URL is '/items?category=books&price=low', what is req.query.price?
Abooks
Blow
Ccategory
Dundefined
✗ Incorrect
The price parameter has the value 'low'.
What type of data is req.query in Express?
AObject
BString
CArray
DNumber
✗ Incorrect
req.query is an object containing key-value pairs from the query string.
If a query parameter is missing, what does req.query.parameter return?
Aerror
Bnull
Cempty string
Dundefined
✗ Incorrect
Missing parameters return undefined in req.query.
Explain how Express.js handles query string parsing and how you access query parameters in a route.
Think about how URLs send extra data and how Express makes it easy to read.
You got /4 concepts.
Describe what happens if a query parameter is not included in the URL and how your Express code should handle it.
Consider what happens when you ask for something that isn't there.
You got /3 concepts.
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
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.
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.
Final Answer:
req.query -> Option A
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
Step 1: Recall query parameter access syntax
Query parameters are accessed via req.query as key-value pairs.
Step 2: Match correct syntax
To get the page parameter, use req.query.page. Other options access wrong objects.
Final Answer:
const page = req.query.page; -> Option A
Quick Check:
Query param 'page' = req.query.page [OK]
Hint: Use req.query.paramName to get query parameters [OK]
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
Step 1: Check for missing query parameter
If term is missing, req.query.term is undefined.
Step 2: Calling method on undefined causes error
Calling toLowerCase() on undefined throws a TypeError.
Final Answer:
TypeError: Cannot read property 'toLowerCase' of undefined -> Option B
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?