0
0
NestJSframework~30 mins

Built-in pipes (ParseIntPipe, ParseBoolPipe) in NestJS - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Built-in Pipes ParseIntPipe and ParseBoolPipe in NestJS
📖 Scenario: You are building a simple NestJS controller to handle user requests where some query parameters need to be converted to specific types automatically.For example, a user might send a query parameter age as a string, but you want it as a number. Similarly, a query parameter active might come as a string but should be treated as a boolean.
🎯 Goal: Create a NestJS controller with two GET routes that use the built-in pipes ParseIntPipe and ParseBoolPipe to automatically convert query parameters to the correct types.This will help ensure your controller methods receive the right data types without manual conversion.
📋 What You'll Learn
Create a controller class named UserController
Add a GET route /user/age that accepts a query parameter age and uses ParseIntPipe to convert it to a number
Add a GET route /user/active that accepts a query parameter active and uses ParseBoolPipe to convert it to a boolean
Use NestJS decorators @Controller, @Get, and @Query properly
Import ParseIntPipe and ParseBoolPipe from @nestjs/common
💡 Why This Matters
🌍 Real World
Web APIs often receive query parameters as strings. Using pipes like ParseIntPipe and ParseBoolPipe helps convert these strings to the correct types automatically, reducing manual parsing and errors.
💼 Career
Understanding and using NestJS built-in pipes is essential for backend developers working with NestJS to build robust and type-safe APIs.
Progress0 / 4 steps
1
Create the UserController class with imports
Create a NestJS controller class named UserController. Import Controller, Get, and Query from @nestjs/common. Do not add any methods yet.
NestJS
Need a hint?

Use @Controller('user') above the class declaration to set the route prefix.

2
Add ParseIntPipe import and age route method
Import ParseIntPipe from @nestjs/common. Add a GET route method named getAge with path 'age'. Use @Query('age', ParseIntPipe) to get the age query parameter as a number. The method should return the age value.
NestJS
Need a hint?

Use @Get('age') above the method and @Query('age', ParseIntPipe) to convert the query parameter.

3
Add ParseBoolPipe import and active route method
Import ParseBoolPipe from @nestjs/common. Add a GET route method named getActive with path 'active'. Use @Query('active', ParseBoolPipe) to get the active query parameter as a boolean. The method should return the active value.
NestJS
Need a hint?

Use @Get('active') and @Query('active', ParseBoolPipe) to convert the query parameter to boolean.

4
Complete the UserController with both routes
Ensure the UserController class includes both getAge and getActive methods with their respective pipes and returns. The class should be decorated with @Controller('user') and import all necessary decorators and pipes from @nestjs/common.
NestJS
Need a hint?

Make sure both methods and imports are present and correctly used.