0
0
Expressframework~10 mins

Query string parsing in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Query string parsing
Client sends URL with query string
Express receives request
Express parses URL
Extract query string part
Parse query string into key-value pairs
Make parsed query available as req.query
Route handler uses req.query values
Express takes the URL from the client, extracts the query string, parses it into an object, and makes it available as req.query for your code 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' parameter from the query string and sends a response showing what was searched.
Execution Table
StepActionInput URLParsed Query ObjectOutput
1Client sends request/search?term=books&sort=ascN/AN/A
2Express receives request/search?term=books&sort=ascN/AN/A
3Express parses URL/search?term=books&sort=ascN/AN/A
4Extract query string/search?term=books&sort=asc{ term: 'books', sort: 'asc' }N/A
5Make query available as req.query/search?term=books&sort=asc{ term: 'books', sort: 'asc' }N/A
6Route handler reads req.query.term/search?term=books&sort=asc{ term: 'books', sort: 'asc' }term = 'books'
7Send response/search?term=books&sort=asc{ term: 'books', sort: 'asc' }You searched for: books
💡 Request handled and response sent with parsed query string values.
Variable Tracker
VariableStartAfter Step 4After Step 6Final
req.queryundefined{ term: 'books', sort: 'asc' }{ term: 'books', sort: 'asc' }{ term: 'books', sort: 'asc' }
termundefinedundefined'books''books'
Key Moments - 3 Insights
Why is req.query an object and not a string?
Because Express automatically parses the query string into an object with keys and values, as shown in step 4 and 5 of the execution_table.
What happens if the query string is empty or missing?
req.query will be an empty object {}, so accessing any key like req.query.term will be undefined, as no query parameters exist.
Can query string values be accessed directly without parsing?
No, Express parses the query string for you. You must use req.query to access parameters, not the raw URL string.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of req.query after step 4?
Aundefined
B{ term: 'books', sort: 'asc' }
Cstring '/search?term=books&sort=asc'
Dnull
💡 Hint
Check the 'Parsed Query Object' column at step 4 in the execution_table.
At which step does the route handler read the 'term' parameter from the query?
AStep 6
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the step where 'term' is assigned a value in the execution_table.
If the URL was '/search' with no query string, what would req.query be?
Aundefined
Bnull
C{} (empty object)
D{ term: undefined }
💡 Hint
Refer to the key_moments section about empty or missing query strings.
Concept Snapshot
Express parses the query string from the URL automatically.
Parsed query parameters are available as an object in req.query.
Access parameters by their key names, e.g., req.query.term.
If no query string, req.query is an empty object {}.
Use req.query inside route handlers to get user input from URLs.
Full Transcript
When a client sends a URL with a query string, 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. Inside your route handler, you can access these parameters by their keys, like req.query.term. If the query string is missing, req.query will be an empty object. This process lets you easily get user input from URLs without manual parsing.