0
0
Expressframework~10 mins

req.query for query strings in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - req.query for query strings
Client sends URL with query string
Express server receives request
Express parses URL
Extract query string part
Parse query string into key-value pairs
Store pairs in req.query object
Route handler accesses req.query
Use query values in response
This flow shows how Express takes the query string from a URL, parses it into an object, and makes it available as req.query for route handlers to use.
Execution Sample
Express
app.get('/search', (req, res) => {
  const term = req.query.term;
  res.send(`You searched for: ${term}`);
});
This code reads the 'term' query parameter from the URL and sends it back in the response.
Execution Table
StepActionInput URLreq.query ContentOutput/Response
1Client sends request/search?term=books{}No response yet
2Express receives request/search?term=books{}No response yet
3Parse query string/search?term=books{ term: 'books' }No response yet
4Route handler reads req.query.term/search?term=books{ term: 'books' }Reads 'books'
5Send response/search?term=books{ term: 'books' }You searched for: books
6Request complete/search?term=books{ term: 'books' }Response sent
💡 Request ends after response is sent to client
Variable Tracker
VariableStartAfter Step 3After Step 4Final
req.query{}{ term: 'books' }{ term: 'books' }{ term: 'books' }
termundefinedundefined'books''books'
Key Moments - 3 Insights
Why is req.query an object and not a string?
Because Express parses the query string into key-value pairs and stores them as properties in req.query, as shown in step 3 of the execution_table.
What happens if the URL has no query string?
req.query will be an empty object {}, so accessing any property like req.query.term will be undefined, as no keys exist.
Can req.query have multiple keys?
Yes, if the URL has multiple query parameters like ?term=books&page=2, req.query will have both keys: { term: 'books', page: '2' }.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What does req.query contain after parsing?
A{ term: 'books' }
B'term=books'
Cundefined
D{}
💡 Hint
Check the 'req.query Content' column at step 3 in the execution_table.
At which step does the route handler read the query parameter value?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column describing when req.query.term is accessed.
If the URL was /search with no query string, what would req.query.term be?
A'search'
Bnull
Cundefined
D{}
💡 Hint
Refer to key_moments about empty query strings and variable_tracker for req.query.
Concept Snapshot
req.query holds query string parameters as an object.
Express parses URL query strings automatically.
Access parameters by name, e.g., req.query.term.
If no query string, req.query is empty {}.
Use in route handlers to customize responses.
Full Transcript
When a client sends a URL with a query string like /search?term=books, Express receives the request and parses the URL. It extracts the query string part after the question mark and converts it into an object with key-value pairs. This object is stored in req.query. For example, req.query will be { term: 'books' }. The route handler can then access req.query.term to get the value 'books' and use it to send a response. If the URL has no query string, req.query will be an empty object, so accessing any key will give undefined. This process allows servers to read extra information from URLs easily and respond accordingly.