📖 Scenario: You are building a simple Express server that responds to requests with personalized messages based on query strings in the URL.
🎯 Goal: Learn how to use req.query in Express to read query string parameters and use them in your response.
📋 What You'll Learn
Create an Express app with a GET route at /greet
Use req.query to read the name parameter from the URL
Send a greeting message that includes the name value
Add a default greeting if name is not provided
💡 Why This Matters
🌍 Real World
Web servers often use query strings to get information from users without needing forms. For example, search pages or filters use query strings.
💼 Career
Understanding how to read query strings with req.query is essential for backend developers working with Express to build APIs and web applications.
Progress0 / 4 steps
1
Set up Express app and route
Create an Express app by requiring express and calling express(). Then create a GET route at /greet with a callback function that takes req and res as parameters.
Express
Hint
Remember to require Express and create an app instance before defining routes.
2
Read the name query parameter
Inside the /greet route callback, create a variable called name and set it to req.query.name to read the name query string parameter.
Express
Hint
Use req.query to access query string parameters by name.
3
Send a greeting message using the name parameter
Still inside the /greet route, use res.send() to send a message that says Hello, {name}! if name exists. If name is missing, send Hello, guest! instead.
Express
Hint
Use an if statement to check if name exists and send the appropriate message.
4
Start the server on port 3000
Add app.listen(3000) to start the Express server on port 3000.
Express
Hint
Use app.listen(3000) to start the server on port 3000.
Practice
(1/5)
1. What does req.query represent in an Express app?
easy
A. An object containing URL query parameters sent after the question mark
B. The body data sent in a POST request
C. The headers of the HTTP request
D. The cookies sent by the client
Solution
Step 1: Understand the role of req.query
req.query holds key-value pairs from the URL query string, which comes after the question mark in a URL.
Step 2: Differentiate from other request parts
Body data is accessed via req.body, headers via req.headers, and cookies via req.cookies. So, req.query is specifically for query parameters.
Final Answer:
An object containing URL query parameters sent after the question mark -> Option A
Quick Check:
req.query = URL query parameters [OK]
Hint: Remember: query = URL after '?' [OK]
Common Mistakes:
Confusing query parameters with body data
Thinking req.query holds headers or cookies
Assuming req.query is a string instead of an object
2. Which of the following is the correct way to access a query parameter named id in Express?
easy
A. req.params.id
B. req.query.id
C. req.body.id
D. req.headers.id
Solution
Step 1: Identify where query parameters are stored
Query parameters are stored in req.query, so to get the value of id, use req.query.id.
Step 2: Differentiate from other request properties
req.params is for route parameters, req.body is for POST data, and req.headers is for HTTP headers.
Final Answer:
req.query.id -> Option B
Quick Check:
Query param 'id' = req.query.id [OK]
Hint: Query params always use req.query.[name] [OK]
Common Mistakes:
Using req.params for query strings
Trying to get query data from req.body
Accessing query params from headers
3. Given this Express route handler, what will be logged if the URL is /search?term=books&page=2?
Hint: Query values come as strings from req.query [OK]
Common Mistakes:
Expecting numbers instead of strings
Confusing query params with route params
Assuming undefined if query exists
4. What is the problem with this Express route handler?
app.get('/items', (req, res) => {
const page = req.query.page;
if (page > 0) {
res.send(`Page number is ${page}`);
} else {
res.send('Invalid page');
}
});
medium
A. res.send cannot be called inside an if statement
B. req.query.page is undefined, causing a crash
C. The route should use req.params instead of req.query
D. req.query.page is a string, so numeric comparison may fail
Solution
Step 1: Identify the type of req.query.page
Query parameters are strings by default, so req.query.page is a string, e.g., "1".
Step 2: Understand comparison behavior
Comparing a string with a number using > can lead to unexpected results because string comparison is lexicographic. The code should convert page to a number first.
Final Answer:
req.query.page is a string, so numeric comparison may fail -> Option D
Quick Check:
Convert query strings to numbers before numeric checks [OK]
Hint: Convert query strings to numbers before numeric comparisons [OK]
Common Mistakes:
Not converting query params to numbers
Assuming req.query.page is a number
Using req.params instead of req.query incorrectly
5. You want to create an Express route that reads optional query parameters sort and limit. If limit is missing or not a positive number, it should default to 10. Which code snippet correctly implements this behavior?