Bird
0
0

Which is the correct way to bind a guard to a specific route in NestJS?

easy📝 Syntax Q12 of 15
NestJS - Guards
Which is the correct way to bind a guard to a specific route in NestJS?
AUse @GlobalGuard decorator on the route
B@UseGuards(MyGuard) above the controller class
CAdd MyGuard to the global providers array
D@UseGuards(MyGuard) above the route handler method
Step-by-Step Solution
Solution:
  1. Step 1: Identify route-level guard binding

    To bind a guard to a specific route, place @UseGuards() decorator above the route handler method.
  2. Step 2: Differentiate from other bindings

    @UseGuards() above the controller binds to all routes in that controller. Global guards are set differently, and @GlobalGuard does not exist.
  3. Final Answer:

    @UseGuards(MyGuard) above the route handler method -> Option D
  4. Quick Check:

    Route guard = @UseGuards on method [OK]
Quick Trick: Use @UseGuards on method for route-level guards [OK]
Common Mistakes:
  • Putting @UseGuards only on controller for route guard
  • Confusing global providers with route guards
  • Using non-existent @GlobalGuard decorator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes