0
0
NestJSframework~10 mins

Pipe binding (parameter, method, controller, global) in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pipe binding (parameter, method, controller, global)
Request Received
Global Pipes Run
Controller Pipes Run
Method Pipes Run
Parameter Pipes Run
Controller Method Executes
Response Sent
The request passes through pipes in order: global, controller, method, then parameter before the controller method runs.
Execution Sample
NestJS
import { Controller, Get, Param, ParseIntPipe, UsePipes, ValidationPipe } from '@nestjs/common';

@UsePipes(new ValidationPipe())
@Controller('items')
export class ItemsController {
  @Get(':id')
  getItem(@Param('id', ParseIntPipe) id: number) {
    return { id };
  }
}
This code shows pipes bound at controller (UsePipes) and parameter (ParseIntPipe) levels.
Execution Table
StepPipe LevelPipe UsedInput ValueOutput ValueAction
1GlobalValidationPipe{ id: '42' }{ id: '42' }Validate input object structure
2ControllerValidationPipe{ id: '42' }{ id: '42' }Validate input again (same pipe instance)
3MethodNone{ id: '42' }{ id: '42' }No method-level pipe, pass through
4ParameterParseIntPipe'42'42Convert string '42' to number 42
5Controller MethodN/Aid=42{ id: 42 }Return object with numeric id
6ResponseN/A{ id: 42 }{ id: 42 }Send response to client
💡 All pipes processed; parameter pipe converted id to number; controller method executed with transformed value.
Variable Tracker
VariableStartAfter Global PipeAfter Controller PipeAfter Method PipeAfter Parameter PipeFinal
input{ id: '42' }{ id: '42' }{ id: '42' }{ id: '42' }4242
idundefinedundefinedundefinedundefined4242
Key Moments - 3 Insights
Why does the ParseIntPipe only run on the parameter and not on the whole request object?
Because parameter pipes apply only to the specific parameter they are bound to, as shown in step 4 of the execution_table where only the 'id' string is converted.
Why does the ValidationPipe run twice in this example?
It runs globally and again at the controller level because it is bound in both places, as seen in steps 1 and 2 of the execution_table.
What happens if a pipe transforms the input incorrectly?
The transformed value is passed to the next step; if invalid, it may cause errors or exceptions before the controller method runs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output value after the Parameter Pipe runs?
A'42'
B42
C{ id: '42' }
Dundefined
💡 Hint
Check step 4 in the execution_table where ParseIntPipe converts the string '42' to number 42.
At which step does the controller method execute?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Step 5 shows the controller method receiving the transformed parameter and returning the response object.
If we remove the global ValidationPipe, which step would be skipped?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Step 1 is the global pipe processing; removing global pipes skips this step.
Concept Snapshot
Pipe binding order in NestJS:
1. Global pipes run first on the whole request.
2. Controller-level pipes run next.
3. Method-level pipes run after.
4. Parameter-level pipes run last on specific parameters.
Each pipe can transform or validate data before the controller method executes.
Full Transcript
In NestJS, pipes can be bound at four levels: global, controller, method, and parameter. When a request arrives, it first passes through global pipes that apply to all routes. Then controller-level pipes run, affecting all methods in that controller. Next, method-level pipes run if defined, and finally, parameter-level pipes run on specific parameters. This layered approach allows precise control over data validation and transformation. For example, a global ValidationPipe can check the whole request structure, while a ParseIntPipe on a parameter converts a string to a number before the controller method uses it. This flow ensures data is clean and correctly typed before business logic runs.