Bird
0
0

Why might you prefer binding a guard at the controller level instead of globally in a NestJS application?

hard📝 Conceptual Q10 of 15
NestJS - Guards
Why might you prefer binding a guard at the controller level instead of globally in a NestJS application?
ATo avoid importing the guard in the main.ts file
BBecause global guards cannot access request data
CTo limit the guard's effect only to specific routes, improving performance and clarity
DBecause controller-level guards run before global guards
Step-by-Step Solution
Solution:
  1. Step 1: Understand scope and impact of guard binding levels

    Global guards apply to all routes, which may be unnecessary and costly.
  2. Step 2: Benefits of controller-level guards

    Binding guards at controller level limits their effect to relevant routes, improving performance and code clarity.
  3. Final Answer:

    To limit the guard's effect only to specific routes, improving performance and clarity -> Option C
  4. Quick Check:

    Controller-level guards limit scope and improve clarity = To limit the guard's effect only to specific routes, improving performance and clarity [OK]
Quick Trick: Use controller guards to scope protection and optimize performance [OK]
Common Mistakes:
  • Thinking global guards cannot access request data
  • Believing controller guards run before global guards
  • Confusing import location with guard binding

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes