0
0
NestJSframework~20 mins

Redirect responses in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redirect Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this NestJS controller method do?

Consider this NestJS controller method:

  @Get('old-route')
  redirectToNew(@Res() res: Response) {
    return res.redirect('/new-route');
  }

What happens when a client requests /old-route?

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

@Controller()
export class AppController {
  @Get('old-route')
  redirectToNew(@Res() res: Response) {
    return res.redirect('/new-route');
  }
}
AThe server throws a runtime error because @Res() is used incorrectly.
BThe client receives a 404 Not Found error.
CThe client receives the content of '/new-route' without redirect.
DThe client receives a 302 redirect to '/new-route'.
Attempts:
2 left
💡 Hint

Think about what res.redirect() does in Express.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses NestJS decorators to redirect?

Which of the following NestJS controller methods correctly sends a redirect response using decorators without accessing @Res()?

A
@Get('go')
redirect() {
  return { url: '/target' };
}
B
@Get('go')
redirect(@Res() res: Response) {
  res.redirect('/target');
}
C
@Get('go')
@Redirect('/target')
redirect() {
  return;
}
D
@Get('go')
@Redirect('/target')
redirect() {
  return '/target';
}
Attempts:
2 left
💡 Hint

Look for the official NestJS way to redirect using decorators.

🔧 Debug
advanced
2:00remaining
Why does this redirect not work as expected?

Given this NestJS controller method:

  @Get('start')
  start(@Res() res: Response) {
    res.redirect('/end');
    return 'done';
  }

What is the problem with this code?

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

@Controller()
export class AppController {
  @Get('start')
  start(@Res() res: Response) {
    res.redirect('/end');
    return 'done';
  }
}
AThe redirect URL '/end' is invalid and causes a 404.
BThe method sends two responses, causing an error.
CThe method works fine and redirects correctly.
DThe return statement is ignored, so the client gets no response.
Attempts:
2 left
💡 Hint

Think about what happens when you call res.redirect() and then return a value.

state_output
advanced
2:00remaining
What is the HTTP status code sent by this redirect?

Consider this NestJS controller method:

  @Get('go')
  @Redirect('/final', 301)
  go() {
    return;
  }

What HTTP status code will the client receive?

NestJS
import { Controller, Get, Redirect } from '@nestjs/common';

@Controller()
export class AppController {
  @Get('go')
  @Redirect('/final', 301)
  go() {
    return;
  }
}
A301 Moved Permanently
B302 Found
C200 OK
D404 Not Found
Attempts:
2 left
💡 Hint

Check the second argument of the @Redirect() decorator.

🧠 Conceptual
expert
3:00remaining
Why should you avoid mixing @Res() and return values in NestJS redirects?

In NestJS, what is the main reason to avoid using @Res() to send a redirect and also returning a value from the same controller method?

ABecause using <code>@Res()</code> disables NestJS automatic response handling, so returning a value will cause an error.
BBecause <code>@Res()</code> only works with JSON responses, not redirects.
CBecause returning a value after <code>@Res()</code> causes the redirect URL to be ignored.
DBecause NestJS requires all redirects to be handled by <code>@Redirect()</code> decorator only.
Attempts:
2 left
💡 Hint

Think about how NestJS handles responses when @Res() is injected.