📖 Scenario: You are building a simple NestJS API that returns a list of books. You want to allow users to filter books by genre using query parameters.
🎯 Goal: Create a NestJS controller that reads a genre query parameter from the URL and returns only books matching that genre.
📋 What You'll Learn
Create a controller with a GET route /books
Use the @Query() decorator to read the genre query parameter
Filter the list of books by the genre parameter if it is provided
Return the filtered list of books as JSON
💡 Why This Matters
🌍 Real World
APIs often need to filter data based on user input in the URL. Query parameters let users specify filters without changing the route.
💼 Career
Backend developers use query parameters to build flexible APIs that serve filtered data, improving user experience and performance.
Progress0 / 4 steps
1
Set up the books data array
Create a variable called books that is an array of objects. Each object should have title and genre properties with these exact entries: { title: 'The Hobbit', genre: 'Fantasy' }, { title: '1984', genre: 'Dystopian' }, { title: 'The Catcher in the Rye', genre: 'Classic' }.
NestJS
Hint
Use const books = [ ... ] to create the array with the exact objects.
2
Create the controller and import decorators
Create a NestJS controller class called BooksController. Import Controller and Get from @nestjs/common. Add the @Controller('books') decorator above the class.
NestJS
Hint
Use @Controller('books') above the class and import the decorators from @nestjs/common.
3
Add a GET method with query parameter
Inside BooksController, add a method called getBooks with the @Get() decorator. Use the @Query('genre') decorator to get the genre query parameter as a string argument. Filter the books array to only include books where book.genre === genre if genre is provided. Return the filtered array or all books if no genre is given.
NestJS
Hint
Import @Query and use it to read the genre query parameter. Use filter to select matching books.
4
Export the controller and finalize
Make sure the BooksController class is exported with export class BooksController. This completes the controller setup to handle query parameters.
NestJS
Hint
Use export class BooksController to make the controller available to the app.
Practice
(1/5)
1. What is the purpose of using @Query() decorator in a NestJS controller?
easy
A. To inject a service into the controller
B. To define a new route path
C. To handle POST request bodies
D. To access query parameters sent in the URL
Solution
Step 1: Understand the role of @Query()
The @Query() decorator extracts query parameters from the URL in a GET request.
Step 2: Compare with other decorators
Other decorators like @Body() handle POST bodies, and @Param() handles route params, so they don't fit here.
Final Answer:
To access query parameters sent in the URL -> Option D
B. The @Query decorator argument should be a string: @Query('search')
C. The parameter name cannot be searchTerm
D. The method should use @Param('search') instead
Solution
Step 1: Check @Query decorator syntax
The argument to @Query() must be a string literal naming the query parameter, e.g., @Query('search').
Step 2: Identify the error in the code
The code uses @Query(search) without quotes, which is invalid syntax and causes an error.
Final Answer:
The @Query decorator argument should be a string: @Query('search') -> Option B
Quick Check:
Decorator argument must be string literal = D [OK]
Hint: Always quote query param name in @Query('name') [OK]
Common Mistakes:
Forgetting quotes around query param name
Using @Param() for query params
Thinking async keyword is disallowed
5. You want to create a NestJS GET endpoint that accepts optional query parameters sort and filter. If filter is missing, it should default to "all". Which code snippet correctly implements this behavior?