Bird
Raised Fist0
NestJSframework~20 mins

Redirect responses in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the primary purpose of using res.redirect() in a NestJS controller?
easy
A. To send the user to a different URL automatically
B. To send JSON data as a response
C. To handle database queries
D. To log user requests

Solution

  1. Step 1: Understand the function of res.redirect()

    This method is used to send an HTTP redirect response to the client, telling the browser to go to a different URL.
  2. 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 of res.redirect().
  3. Final Answer:

    To send the user to a different URL automatically -> Option A
  4. Quick Check:

    Redirect means sending user to another URL [OK]
Hint: Redirect means sending user to a new URL [OK]
Common Mistakes:
  • Confusing redirect with sending JSON response
  • Thinking redirect handles database operations
  • Assuming redirect logs requests
2. Which of the following is the correct way to inject the response object to use res.redirect() in a NestJS controller method?
easy
A. Use @Body() decorator
B. Use @Req() decorator
C. Use @Res() decorator
D. Use @Param() decorator

Solution

  1. Step 1: Identify the decorator for response object

    In NestJS, @Res() is used to inject the response object to access methods like redirect().
  2. 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.
  3. Final Answer:

    Use @Res() decorator -> Option C
  4. Quick Check:

    Response object injection = @Res() [OK]
Hint: Use @Res() to get response object for redirect [OK]
Common Mistakes:
  • Using @Req() instead of @Res() for response
  • Confusing @Body() with response injection
  • Using @Param() which is for route params
3. Consider this NestJS controller method:
async redirectToGoogle(@Res() res) {
  res.redirect('https://google.com');
}
What will happen when this method is called?
medium
A. The user will be redirected to 'https://google.com' with status 302
B. The user will receive a JSON response with the URL
C. The server will throw an error because status code is missing
D. Nothing happens because redirect needs a status code

Solution

  1. 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.
  2. Step 2: Check if status code is mandatory

    Status code is optional; if omitted, 302 is used automatically, so no error occurs.
  3. Final Answer:

    The user will be redirected to 'https://google.com' with status 302 -> Option A
  4. Quick Check:

    Redirect URL alone defaults to 302 status [OK]
Hint: Redirect without status defaults to 302 [OK]
Common Mistakes:
  • Thinking status code is required for redirect
  • Expecting JSON response instead of redirect
  • Assuming server error without status code
4. Identify the error in this NestJS controller method:
@Get('go')
async go(@Res() res) {
  res.redirect(301, 'https://example.com');
  return 'Redirecting';
}
medium
A. Status code 301 is invalid for redirect
B. Returning a string after res.redirect() causes an error
C. Missing await before res.redirect()
D. res.redirect() must be called without parameters

Solution

  1. Step 1: Understand res.redirect() behavior

    Calling res.redirect() sends the response immediately. Returning a value after sending response causes NestJS to throw an error because response is already sent.
  2. Step 2: Check other options

    Status code 301 is valid for permanent redirect, no await needed as redirect is synchronous, and redirect requires parameters.
  3. Final Answer:

    Returning a string after res.redirect() causes an error -> Option B
  4. Quick Check:

    Don't return after sending response [OK]
Hint: Don't return after res.redirect() sends response [OK]
Common Mistakes:
  • Thinking 301 is invalid status code
  • Adding await unnecessarily
  • Calling redirect without parameters
5. You want to create a NestJS controller method that redirects users to different URLs based on a query parameter 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?
hard
A. 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('/'); } }
B. 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'; }
C. 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, '/'); } }
D. 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('/'); } }

Solution

  1. Step 1: Check correct usage of res.redirect()

    Calling res.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.
  2. 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.
  3. Final Answer:

    The code that calls res.redirect() properly without return and with correct status codes -> Option D
  4. Quick Check:

    Call res.redirect() without return, specify status if needed [OK]
Hint: Call res.redirect() without return; specify status as first argument [OK]
Common Mistakes:
  • Returning after res.redirect() causing errors
  • Not specifying status code correctly
  • Returning strings after sending response
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard