Consider a NestJS controller method where a custom pipe is applied only to a single parameter. What will be the effect of this pipe binding?
import { Controller, Get, Param, ParseIntPipe } from '@nestjs/common'; @Controller('items') export class ItemsController { @Get(':id') getItem(@Param('id', ParseIntPipe) id: number) { return { id, type: typeof id }; } }
Think about where the pipe is applied and what it affects.
When a pipe is bound at the parameter level, it only affects that specific parameter in that method. Here, ParseIntPipe converts the 'id' string parameter to a number before the method uses it.
Given a NestJS controller with a pipe applied at the controller level, how does this pipe affect incoming requests?
import { Controller, Get, Param, UsePipes, ParseIntPipe } from '@nestjs/common'; @UsePipes(ParseIntPipe) @Controller('users') export class UsersController { @Get(':id') getUser(@Param('id') id: number) { return { id, type: typeof id }; } @Get('profile/:id') getProfile(@Param('id') id: number) { return { profileId: id, type: typeof id }; } }
Consider the scope of the @UsePipes decorator when applied to a controller.
When a pipe is bound at the controller level using @UsePipes, it applies to all route handler methods within that controller. Here, ParseIntPipe converts all parameters to numbers for every method.
In a NestJS application, a pipe is bound globally in the main.ts file. How does this affect the request handling?
import { ValidationPipe } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.useGlobalPipes(new ValidationPipe()); await app.listen(3000); } bootstrap();
Think about what 'global' means in this context.
Binding a pipe globally with app.useGlobalPipes applies it to every request in the app, regardless of controller or method. This ensures consistent validation or transformation everywhere.
Choose the correct way to apply a custom pipe named MyCustomPipe only to the create method of a controller.
import { Controller, Post, Body, UsePipes } from '@nestjs/common'; import { MyCustomPipe } from './my-custom.pipe'; @Controller('products') export class ProductsController { @Post() create(@Body() data: any) { return data; } }
Remember where to place the @UsePipes decorator to affect only one method.
To apply a pipe only to a specific method, place @UsePipes above that method. Option C correctly applies MyCustomPipe only to the create method.
Given the following NestJS setup, why does the global pipe not transform the incoming string to a number?
import { PipeTransform, Injectable, ArgumentMetadata } from '@nestjs/common'; @Injectable() export class ToNumberPipe implements PipeTransform { transform(value: any, metadata: ArgumentMetadata) { return Number(value); } } // main.ts import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.useGlobalPipes(new ToNumberPipe()); await app.listen(3000); } bootstrap(); // Controller method import { Controller, Get, Param } from '@nestjs/common'; @Controller('orders') export class OrdersController { @Get(':id') getOrder(@Param('id') id: number) { return { id, type: typeof id }; } }
Consider how NestJS handles parameter transformation and when pipes run.
Global pipes run on the entire request body by default but do not automatically transform route parameters unless the pipe is explicitly set to transform parameters or the parameter decorator supports it. Here, @Param does not trigger transformation, so the id remains a string.