Handling Query Parameters in NestJS
📖 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
/booksUse the
@Query() decorator to read the genre query parameterFilter the list of books by the
genre parameter if it is providedReturn 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