Bird
0
0

Given this code snippet, what will happen when a request hits the GET /users/profile route?

medium📝 component behavior Q13 of 15
NestJS - Guards
Given this code snippet, what will happen when a request hits the GET /users/profile route?
@UseGuards(GlobalGuard)
@Controller('users')
@UseGuards(ControllerGuard)
export class UserController {
  @Get('profile')
  @UseGuards(RouteGuard)
  getProfile() {
    return 'User Profile';
  }
}
AGlobalGuard, ControllerGuard, and RouteGuard all run in order.
BOnly GlobalGuard runs before the route handler.
COnly RouteGuard runs, ignoring others.
DControllerGuard overrides and disables other guards.
Step-by-Step Solution
Solution:
  1. Step 1: Identify guard binding levels in code

    GlobalGuard is bound globally, ControllerGuard on the controller, and RouteGuard on the route method.
  2. Step 2: Understand guard execution order

    All guards bound at different levels run in order: global first, then controller, then route.
  3. Final Answer:

    GlobalGuard, ControllerGuard, and RouteGuard all run in order. -> Option A
  4. Quick Check:

    All guards run in binding order [OK]
Quick Trick: All guards at different levels run in sequence [OK]
Common Mistakes:
  • Assuming only one guard runs
  • Thinking controller guard disables others
  • Believing global guard runs alone

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes