NestJS - GuardsWhich is the correct way to bind a guard to a specific route in NestJS?AUse @GlobalGuard decorator on the routeB@UseGuards(MyGuard) above the controller classCAdd MyGuard to the global providers arrayD@UseGuards(MyGuard) above the route handler methodCheck Answer
Step-by-Step SolutionSolution:Step 1: Identify route-level guard bindingTo bind a guard to a specific route, place @UseGuards() decorator above the route handler method.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.Final Answer:@UseGuards(MyGuard) above the route handler method -> Option DQuick 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 guardConfusing global providers with route guardsUsing non-existent @GlobalGuard decorator
Master "Guards" in NestJS9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More NestJS Quizzes Authentication - Local strategy (username/password) - Quiz 6medium Database with TypeORM - Repository pattern - Quiz 12easy Database with TypeORM - Transactions - Quiz 1easy Database with TypeORM - Repository pattern - Quiz 15hard Database with TypeORM - CRUD operations - Quiz 11easy Guards - Combining multiple guards - Quiz 4medium Interceptors - Why interceptors add cross-cutting logic - Quiz 8hard Interceptors - Response transformation - Quiz 14medium Interceptors - Timeout interceptor - Quiz 5medium Pipes - Pipe binding (parameter, method, controller, global) - Quiz 9hard