Skip to content

RedirectResponse

reference
1 min readUpdated

Kind: Interface

Source: packages/core/router/router-response-controller.ts

Part of: Core

RedirectResponse represents the data required to issue an HTTP redirect from the router response controller. It pairs a destination URL with the HTTP status code that determines how clients should handle the redirect, such as 302 for temporary redirects or 301 for permanent redirects.

Properties

PropertyType
urlstring
statusCodenumber

Diagram

mermaid
graph LR
  Router[Router Handler] --> ResponseController[Router Response Controller]
  ResponseController --> RedirectResponse
  RedirectResponse --> URL[url: string]
  RedirectResponse --> Status[statusCode: number]
  ResponseController --> HTTPRedirect[HTTP Redirect Response]

Usage

ts
import type { RedirectResponse } from './router-response-controller';

const redirectToLogin: RedirectResponse = {
  url: '/login?returnUrl=%2Fdashboard',
  statusCode: 302,
};

// Pass the redirect response to the router response controller.
responseController.redirect(redirectToLogin);

AI Coding Instructions

  • Always provide a valid redirect target in url; preserve or encode query parameters when building dynamic URLs.
  • Use an appropriate HTTP redirect status code: 301/308 for permanent redirects and 302/307 for temporary redirects.
  • Keep redirect construction within routing or response-controller logic rather than returning raw redirect objects from unrelated services.
  • Validate or constrain externally supplied redirect URLs to prevent open redirect vulnerabilities.

Was this page helpful?

Download as PDF
RedirectResponse — NestJS head-to-head