0
0
NestJSframework~10 mins

Query parameters in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Query parameters
Client sends HTTP GET request
NestJS Controller receives request
Extract query parameters using @Query()
Use query parameters in handler logic
Send response back to client
The flow shows how a NestJS controller extracts query parameters from an HTTP GET request and uses them in the handler.
Execution Sample
NestJS
import { Controller, Get, Query } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  @Get()
  findAll(@Query('search') search: string) {
    return `Searching for: ${search}`;
  }
}
This code defines a GET endpoint that reads the 'search' query parameter and returns a message including it.
Execution Table
StepActionRequest URLQuery ExtractedHandler Output
1Client sends GET request/items?search=bookN/AN/A
2NestJS receives request/items?search=booksearch=bookN/A
3Controller method called/items?search=booksearch=bookN/A
4Return response/items?search=booksearch=bookSearching for: book
5Response sent to client/items?search=booksearch=bookSearching for: book
💡 Request handled and response sent back to client with query parameter value.
Variable Tracker
VariableStartAfter Request
searchundefined"book"
Key Moments - 2 Insights
Why do we use @Query('search') instead of just @Query()?
Using @Query('search') extracts only the 'search' parameter from the query string, as shown in execution_table step 3. Using @Query() alone would give all query parameters as an object.
What happens if the query parameter is missing?
If 'search' is missing, the variable 'search' will be undefined (see variable_tracker start value). The handler can check for this and handle it accordingly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value of the 'search' query parameter extracted?
A"items"
B"book"
Cundefined
D"search"
💡 Hint
Check the 'Query Extracted' column at step 2 in the execution_table.
At which step does the controller method receive the query parameter value?
AStep 3
BStep 1
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column to find when the controller method is called.
If the URL was /items without any query, what would be the value of 'search' in variable_tracker?
A"" (empty string)
Bnull
Cundefined
D0
💡 Hint
Refer to variable_tracker start value for 'search' when no query parameter is provided.
Concept Snapshot
NestJS Query Parameters:
- Use @Query('paramName') to get a single query parameter.
- The parameter is extracted from the URL after '?'.
- If missing, the value is undefined.
- Use in GET handlers to customize responses.
- Example: /items?search=book extracts 'book' as search.
Full Transcript
In NestJS, query parameters come from the URL after the question mark. The controller uses the @Query decorator to get these values. For example, @Query('search') gets the 'search' parameter. When a client sends a GET request like /items?search=book, NestJS extracts 'book' and passes it to the handler. If the parameter is missing, the value is undefined. The handler can then use this value to customize the response. This flow helps build flexible APIs that respond to client queries.