Using Default Value Pipe in NestJS
📖 Scenario: You are building a simple NestJS controller to handle user queries. Sometimes users may not provide a query parameter, so you want to set a default value automatically.
🎯 Goal: Create a NestJS controller with a GET endpoint that uses the DefaultValuePipe to provide a default value for a query parameter called page.
📋 What You'll Learn
Create a controller class named
AppController.Add a GET route handler method named
getItems.Use the
@Query decorator with DefaultValuePipe to set a default value of 1 for the page query parameter.Return the
page value as a number from the handler.💡 Why This Matters
🌍 Real World
Setting default values for query parameters is common in APIs to handle missing inputs gracefully.
💼 Career
Understanding pipes like DefaultValuePipe is essential for building robust and user-friendly APIs in NestJS.
Progress0 / 4 steps