0
0
NestJSframework~10 mins

Pipe binding (parameter, method, controller, global) in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to bind a pipe to a single parameter in a NestJS controller method.

NestJS
async getUser(@Param('id', [1]) id: number) { return this.userService.findById(id); }
Drag options to blanks, or click blank then click option'
Aid
BParseIntPipe
CBody
DQuery
Attempts:
3 left
💡 Hint
Common Mistakes
Selecting 'id' (the parameter name) instead of a pipe like ParseIntPipe
Confusing pipes with other decorators like @Body or @Query
2fill in blank
medium

Complete the code to apply a pipe to an entire controller method.

NestJS
@UsePipes([1])
async create(@Body() data: CreateDto) { return this.service.create(data); }
Drag options to blanks, or click blank then click option'
AController
BParseIntPipe
CHttpException
DValidationPipe
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class that is not a pipe inside @UsePipes
Confusing @UsePipes with @UseGuards or other decorators
3fill in blank
hard

Fix the error in binding a pipe globally in the main.ts file.

NestJS
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new [1]());
  await app.listen(3000);
}
Drag options to blanks, or click blank then click option'
AValidationPipe
BParseIntPipe
CHttpException
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-pipe classes like Controller or HttpException
Forgetting to instantiate the pipe with 'new'
4fill in blank
hard

Fill both blanks to bind a pipe to a controller and a method.

NestJS
@UsePipes([1])
@Controller('items')
export class ItemsController {
  @UsePipes([2])
  async update(@Param('id', ParseIntPipe) id: number) {
    return this.itemsService.update(id);
  }
}
Drag options to blanks, or click blank then click option'
AValidationPipe
BParseIntPipe
CHttpException
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-pipe classes in @UsePipes
Mixing up controller and method pipe usage
5fill in blank
hard

Fill all three blanks to create a custom pipe and bind it globally.

NestJS
import { PipeTransform, Injectable, ArgumentMetadata } from '@nestjs/common';

@Injectable()
export class [1] implements PipeTransform {
  transform(value: any, metadata: ArgumentMetadata) {
    if (!value) {
      throw new [2]('Value is required');
    }
    return value.trim();
  }
}

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new [3]());
  await app.listen(3000);
}
Drag options to blanks, or click blank then click option'
ATrimPipe
BBadRequestException
DValidationPipe
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong exception class
Not instantiating the pipe with 'new' when binding globally
Mismatching class names