0PricingLogin
NestJS Enterprise Backend APIs · Lesson

Reading Request Context with Param Decorators

Create custom @CurrentUser and @ClientIp param decorators using createParamDecorator and ExecutionContext.

Why Param Decorators?

In NestJS controllers, you often need to pull the same piece of data out of the request over and over: the authenticated user, the client IP, a tenant id from a header. Doing this with @Req() and digging into request.user in every handler is repetitive and leaks framework details into your business logic.

Custom param decorators let you encapsulate that extraction once and reuse it everywhere:

  • @CurrentUser() instead of req.user
  • @ClientIp() instead of parsing x-forwarded-for

The result is cleaner, more testable, and more declarative controller code.

The Repetitive Way

Here is what you usually start with: grabbing the whole request object and reaching into it manually. It works, but every handler repeats the same lines, and the controller now knows about request.user, which is an implementation detail of your auth guard.

Notice how the actual route logic is buried under plumbing. This is exactly what a param decorator removes.

import { Controller, Get, Req } from '@nestjs/common';
import { Request } from 'express';

@Controller('profile')
export class ProfileController {
  @Get()
  getProfile(@Req() request: Request) {
    const user = (request as any).user;
    return { id: user.id, email: user.email };
  }
}

All lessons in this course

  1. Reading Request Context with Param Decorators
  2. Attaching Metadata with SetMetadata and Reflector
  3. Composing Decorators with applyDecorators
  4. Class-Level Decorators for Cross-Cutting Config
← Back to NestJS Enterprise Backend APIs