Redirection in Nest.js

Shameel Uddin - Jan 1 - - Dev Community

Understanding HTTP Redirection: A Brief Overview

HTTP redirection is a mechanism used by web servers to instruct browsers to navigate from one URL to another. This process is crucial for various scenarios, such as when a page has permanently moved or when a temporary redirect is needed. Two common status codes used for redirection are 301 (Moved Permanently) and 302 (Found or Moved Temporarily).

  • 301 (Moved Permanently):

    • A 301 status code indicates that the requested resource has been permanently moved to a new location.
    • Browsers and search engines interpret this as a permanent change, and they update their records accordingly.
    • This is useful when you want to divert your traffic to your new URL.
  • 302 (Found or Moved Temporarily):

    • A 302 status code indicates that the requested resource has been temporarily moved to a different location.
    • Browsers understand this as a temporary redirection and may continue to use the original URL for future requests.
    • This is beneficial when you need a temporary redirect, such as during maintenance or when testing a new page.

Implementing Redirection in Nest.js

Now, let's delve into the provided Nest.js code that demonstrates how to implement redirection using these HTTP status codes.

Video Explanation:

Since you are following a series so your Nest.js project should be up and running.

Create a new controller by the name of redirect

nest g controller redirect
Enter fullscreen mode Exit fullscreen mode

Paste the following code in src/redirect.

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

@Controller('redirect')
export class RedirectController {
  @Get('youtube')
  @Redirect('https://www.youtube.com/@ShameelUddin123', 301) //permenant
  redirectToNewUrl301() {
    console.log('Permenant!');
  }

  @Get('github')
  @Redirect('https://github.com/shameel123', 302) //temporary
  redirectToTemporaryUrl302() {
    console.log('Temporary!');
  }
}

Enter fullscreen mode Exit fullscreen mode

Code Explanation:

  1. Decorator Usage:

    • The @Controller decorator is used to define a controller for handling HTTP requests related to redirection.
    • The @Get decorator specifies that the following method should handle GET requests for the specified route.
  2. Redirection Implementation:

    • The @Redirect decorator is employed to specify the target URL and the HTTP status code for redirection.
    • In the redirectToNewUrl301 method, a 301 status code is used for a permanent redirect to the YouTube channel.
    • In the redirectToTemporaryUrl302 method, a 302 status code is used for a temporary redirect to the GitHub profile.
  3. Code Execution:

    • It's important to note that the code block following the redirection decorator (return { url: 'https://www.google.com/' };) will be executed in 302 but not in 301.

Execution

When you hit in browser:

localhost:3000/redirect/youtube
Enter fullscreen mode Exit fullscreen mode

You will be navigated to my YouTube channel.

But, when you hit this in browser:

localhost:3000/redirect/youtube
Enter fullscreen mode Exit fullscreen mode

Then, you would rather be re-directed to Google page. This is because in 302, the method below @Redirect() is executed so make sure that you early return it in case the method has some code which is under development so that the users can successfully (temporarily) be navigated to updated URL.

Update it like this:

@Get('github')
  @Redirect('https://github.com/shameel123', 302) //temporary
  redirectToTemporaryUrl302() {
  return
    //This will be executed!
    return { url: 'https://www.google.com/' };
  }
Enter fullscreen mode Exit fullscreen mode

By understanding HTTP redirection and reviewing this Nest.js code, developers can effectively manage URL transitions, ensuring a smooth user experience and proper handling of changes in website structure.

Follow me for more such content:
YouTube: https://www.youtube.com/@ShameelUddin123
LinkedIn: https://www.linkedin.com/in/shameeluddin/
Github: https://github.com/Shameel123

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player