0
0
NestJSframework~10 mins

Redirect responses in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to send a redirect response to '/home' using NestJS.

NestJS
return response.[1]('/home');
Drag options to blanks, or click blank then click option'
Asend
Bredirect
Cjson
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'redirect' causes the server to send data instead of redirecting.
Using 'status' alone does not perform a redirect.
2fill in blank
medium

Complete the code to inject the response object in a NestJS controller method.

NestJS
import { Response } from 'express';

@Get()
handleRequest(@[1]() response: Response) {
  return response.redirect('/dashboard');
}
Drag options to blanks, or click blank then click option'
ARes
BBody
CReq
DQuery
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@Req()' instead of '@Res()' injects the request, not the response.
Using '@Body()' or '@Query()' injects parts of the request, not the response.
3fill in blank
hard

Fix the error in the code to properly redirect with status 301 in NestJS.

NestJS
return response.status([1]).redirect('/new-url');
Drag options to blanks, or click blank then click option'
A301
B200
C404
D500
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 status code does not perform a redirect.
Using 404 or 500 causes errors instead of redirect.
4fill in blank
hard

Fill both blanks to create a redirect response with status 302 and URL '/login'.

NestJS
return response.status([1]).[2]('/login');
Drag options to blanks, or click blank then click option'
A302
Bredirect
Csend
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'redirect' does not redirect.
Using status 404 is not a redirect.
5fill in blank
hard

Fill all three blanks to create a NestJS controller method that redirects to '/profile' with status 307.

NestJS
import { Controller, Get, Res } from '@nestjs/common';
import { Response } from 'express';

@Controller('user')
export class UserController {
  @Get('redirect')
  redirectToProfile(@[1]() response: Response) {
    return response.status([2]).[3]('/profile');
  }
}
Drag options to blanks, or click blank then click option'
ARes
B307
Credirect
DReq
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@Req()' instead of '@Res()' injects request, not response.
Using wrong status codes like 200 or 404.
Using 'send' instead of 'redirect' method.