Redirect responses send users to a different URL automatically. This helps guide users to the right page without them clicking links.
Redirect responses in NestJS
import { Controller, Get, Res } from '@nestjs/common'; import { Response } from 'express'; @Controller() export class AppController { @Get('old-path') redirectToNew(@Res() res: Response) { return res.redirect('/new-path'); } }
Use the @Res() decorator to access the response object.
Call res.redirect() with the URL to send a redirect response.
res.redirect('/home');res.redirect(301, '/new-location');
res.redirect('https://example.com');This controller has two routes. Visiting '/old-page' sends a redirect response to '/new-page'. Visiting '/new-page' shows a welcome message.
import { Controller, Get, Res } from '@nestjs/common'; import { Response } from 'express'; @Controller() export class RedirectController { @Get('old-page') redirectToNewPage(@Res() res: Response) { return res.redirect('/new-page'); } @Get('new-page') newPage() { return 'Welcome to the new page!'; } }
Using @Res() disables automatic response handling, so you must send the response yourself.
Redirect status codes like 301 (permanent) or 302 (temporary) can be specified as the first argument.
Always test redirects in a browser or with tools like curl to confirm behavior.
Redirect responses send users to a different URL automatically.
Use res.redirect() inside a controller method with @Res().
You can specify the redirect URL and optionally the status code.
Practice
res.redirect() in a NestJS controller?Solution
Step 1: Understand the function of
This method is used to send an HTTP redirect response to the client, telling the browser to go to a different URL.res.redirect()Step 2: Compare with other options
Options A, B, and C describe other unrelated tasks like logging, sending JSON, or querying databases, which are not the purpose ofres.redirect().Final Answer:
To send the user to a different URL automatically -> Option AQuick Check:
Redirect means sending user to another URL [OK]
- Confusing redirect with sending JSON response
- Thinking redirect handles database operations
- Assuming redirect logs requests
res.redirect() in a NestJS controller method?Solution
Step 1: Identify the decorator for response object
In NestJS,@Res()is used to inject the response object to access methods likeredirect().Step 2: Verify other decorators
@Req()is for request,@Body()is for request body data, and@Param()is for route parameters, none provide the response object.Final Answer:
Use@Res()decorator -> Option CQuick Check:
Response object injection = @Res() [OK]
- Using @Req() instead of @Res() for response
- Confusing @Body() with response injection
- Using @Param() which is for route params
async redirectToGoogle(@Res() res) {
res.redirect('https://google.com');
}
What will happen when this method is called?Solution
Step 1: Understand default behavior of res.redirect()
When called with only a URL,res.redirect()sends a 302 status code by default, redirecting the user to the given URL.Step 2: Check if status code is mandatory
Status code is optional; if omitted, 302 is used automatically, so no error occurs.Final Answer:
The user will be redirected to 'https://google.com' with status 302 -> Option AQuick Check:
Redirect URL alone defaults to 302 status [OK]
- Thinking status code is required for redirect
- Expecting JSON response instead of redirect
- Assuming server error without status code
@Get('go')
async go(@Res() res) {
res.redirect(301, 'https://example.com');
return 'Redirecting';
}Solution
Step 1: Understand res.redirect() behavior
Callingres.redirect()sends the response immediately. Returning a value after sending response causes NestJS to throw an error because response is already sent.Step 2: Check other options
Status code 301 is valid for permanent redirect, no await needed as redirect is synchronous, and redirect requires parameters.Final Answer:
Returning a string after res.redirect() causes an error -> Option BQuick Check:
Don't return after sending response [OK]
- Thinking 301 is invalid status code
- Adding await unnecessarily
- Calling redirect without parameters
type. If type is 'admin', redirect to '/admin/dashboard' with status 302; if 'user', redirect to '/user/home' with status 301; otherwise, redirect to '/' with status 302. Which code snippet correctly implements this?Solution
Step 1: Check correct usage of res.redirect()
Callingres.redirect()with URL only defaults to 302 status. To specify status, pass it as first argument. The method sends response immediately, so no return is needed.Step 2: Analyze each option
async redirect(@Query('type') type: string, @Res() res) { if (type === 'admin') { return res.redirect('/admin/dashboard'); } else if (type === 'user') { return res.redirect(301, '/user/home'); } else { return res.redirect('/'); } } incorrectly uses return res.redirect(), which can cause errors. async redirect(@Query('type') type: string, @Res() res) { if (type === 'admin') { res.redirect(302, '/admin/dashboard'); } else if (type === 'user') { res.redirect('/user/home'); } else { res.redirect(302, '/'); } return 'Redirected'; } returns a string after redirect, causing errors. async redirect(@Query('type') type: string, @Res() res) { if (type === 'admin') { return res.redirect(302, '/admin/dashboard'); } else if (type === 'user') { return res.redirect('/user/home'); } else { return res.redirect(302, '/'); } } mixes return and missing status codes. async redirect(@Query('type') type: string, @Res() res) { if (type === 'admin') { res.redirect('/admin/dashboard'); } else if (type === 'user') { res.redirect(301, '/user/home'); } else { res.redirect('/'); } } calls res.redirect() properly without return and uses correct status codes.Final Answer:
The code that calls res.redirect() properly without return and with correct status codes -> Option DQuick Check:
Call res.redirect() without return, specify status if needed [OK]
- Returning after res.redirect() causing errors
- Not specifying status code correctly
- Returning strings after sending response
Ready to prove it?
Pass the test and this pattern moves unknown
