Skip to content

Req

reference
1 min readUpdated

Kind: Constant

Source: packages/common/decorators/http/route-params.decorator.ts

Part of: Common

Route handler parameter decorator. Extracts the Request object from the underlying platform and populates the decorated parameter with the value of Request.

Alias for

Req is a route handler parameter decorator that injects the underlying platform request object into a controller method parameter. It is typically used when access to raw request details—such as headers, cookies, URL parameters, or platform-specific properties—is needed.

Definition

ts
Request

Value

ts
Request

Diagram

mermaid
graph LR
  A[Incoming HTTP Request] --> B[Platform Adapter]
  B --> C[Controller Route Handler]
  C --> D[@Req() Decorated Parameter]
  D --> E[Request Object]

Usage

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

@Controller('users')
export class UsersController {
  @Get('profile')
  getProfile(@Req() request: Request) {
    return {
      userAgent: request.headers['user-agent'],
      path: request.path,
    };
  }
}

AI Coding Instructions

  • Use @Req() only when the full underlying request object is required; prefer focused decorators such as @Body(), @Param(), or @Headers() for specific values.
  • Type the parameter using the request type provided by the active HTTP platform, such as Express Request or Fastify FastifyRequest.
  • Avoid coupling business logic directly to the raw request object; extract required values in the controller and pass them to services.
  • Remember that request properties may vary between HTTP adapters, so avoid relying on adapter-specific fields unless the application is intentionally platform-specific.

Used by

6 references from 6 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (6)

  • AppControllerintegration/nest-application/app-locals/src/app.controller.ts:4
  • AppControllerintegration/nest-application/global-prefix/src/app.controller.ts:3
  • ExpressControllerintegration/nest-application/raw-body/src/express.controller.ts:4
  • FastifyControllerintegration/nest-application/raw-body/src/fastify.controller.ts:4
  • PassthroughInterceptorintegration/nest-application/sse/src/app.controller.ts:28
  • AppControllerintegration/nest-application/use-body-parser/src/app.controller.ts:4

Was this page helpful?

Download as PDF
Req — NestJS head-to-head