NestJS

Nest UseGuards

Guards decide whether a handler may run, interceptors wrap handler execution, and exception filters map uncaught exceptions to responses. For ordinary HTTP flow, middleware precedes guards, then interceptors enter, pipes resolve arguments and the controller executes. Interceptors can map returned values on the way out. Exception filters handle uncaught failures; they are not an unconditional final stage. Guards may reject before the handler, and interceptors may short-circuit. This fixture uses a denying guard only, to illustrate the boundary without real authentication. Static example only; runtime NOT RUN. Source: https://docs.nestjs.com/faq/request-lifecycle.

Formal Syntax & Structure

nodejs syntaxnodejs
@UseGuards(GuardClass)

Working Example

Example Snippetnodejs
import { CanActivate, Controller, Get, Injectable, UseGuards } from '@nestjs/common';@Injectable()class DenyDemoGuard implements CanActivate {  canActivate() { return false; }}@Controller('staff')@UseGuards(DenyDemoGuard)export class StaffController {  @Get('stock') stock() { return { count:4 }; }}
Node.js Reference