0
0
NestJSframework~10 mins

Redirect responses in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Redirect responses
Client sends request
Controller method called
Decide to redirect
Send redirect response with status and URL
Client receives redirect
Client requests new URL
The server receives a request, decides to redirect, sends a redirect response, and the client follows the new URL.
Execution Sample
NestJS
import { Controller, Get, Res } from '@nestjs/common';
import { Response } from 'express';

@Controller()
export class AppController {
  @Get('old')
  redirect(@Res() res: Response) {
    return res.redirect(301, '/new');
  }
}
This code redirects requests from '/old' to '/new' with a 301 status.
Execution Table
StepActionInput/ConditionResponse SentClient Behavior
1Client sends GET request to '/old'Request URL: /oldNo response yetWaiting
2NestJS calls AppController.redirectRoute matches '/old'No response yetWaiting
3Controller calls res.redirect(301, '/new')Redirect status 301, URL '/new'HTTP 301 with Location: /newReceives redirect
4Client receives 301 redirectStatus 301, Location '/new'No new response yetSends GET request to '/new'
5Client requests '/new'Request URL: /newDepends on '/new' handlerFollows new URL
6Process ends for this redirectRedirect completeFinal response from '/new'Displays new page
💡 Redirect response sent with status 301 and Location header; client follows new URL
Variable Tracker
VariableStartAfter Step 3After Step 4Final
resExpress Response objectRedirect response prepared with status 301 and Location '/new'Redirect response sentResponse cycle complete
Key Moments - 3 Insights
Why does the client send a new request after receiving the redirect?
Because the server sends a 301 status with a Location header (see execution_table step 3), the client knows to request the new URL.
What happens if we don't use @Res() and just return a string?
NestJS treats it as a normal response and does not redirect. Using @Res() lets us control the response directly (see execution_table step 3).
Can we change the redirect status code?
Yes, the first argument to res.redirect() is the status code (e.g., 301 or 302), controlling how the client handles the redirect (see execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what response does the server send at step 3?
AHTTP 404 Not Found
BHTTP 200 with body 'Redirected'
CHTTP 301 with Location header '/new'
DNo response sent yet
💡 Hint
Check the 'Response Sent' column at step 3 in execution_table
At which step does the client send the new request to '/new'?
AStep 5
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Client Behavior' column for when the client requests '/new'
If we change the status code in res.redirect(301, '/new') to 302, what changes in the execution?
AThe client does not redirect
BThe client receives a 302 redirect instead of 301
CThe server sends a 404 error
DThe redirect URL changes automatically
💡 Hint
Refer to the 'Action' and 'Response Sent' columns in execution_table step 3
Concept Snapshot
NestJS redirect responses:
- Use @Res() to access Express Response
- Call res.redirect(statusCode, url) to send redirect
- Client receives status and Location header
- Client follows new URL automatically
- Common status codes: 301 (permanent), 302 (temporary)
Full Transcript
In NestJS, redirect responses happen when the server tells the client to go to a different URL. The client sends a request, the controller method runs, and if a redirect is needed, it uses the Express Response object's redirect method with a status code and new URL. The server sends back a response with that status and a Location header. The client then automatically requests the new URL. This process involves the controller method calling res.redirect(301, '/new'), sending a 301 status code and Location header. The client receives this and sends a new request to '/new'. This is how redirect responses work step-by-step in NestJS.