DEV Community

Shameel Uddin
Shameel Uddin

Posted on

Redirection in Nest.js

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

Top comments (10)

Collapse
 
webjose profile image
José Pablo Ramírez Vargas
Collapse
 
shameel profile image
Shameel Uddin

@webjose

I found this pretty offensive (sorry to say)

I recorded the video as well as wrote the content with the help.

Use of AI assistant has been done in video as well if you watch closely as I utilize GitHub Co-pilot in the series. :)

Collapse
 
webjose profile image
José Pablo Ramírez Vargas • Edited

Whether or not you find it offensive, seems that the guideline applies regardless. I am merely providing you with the link so you can follow the guidelines and your content doesn't get flagged. Cheers.

I also see the artificial upvoting, BTW. Is it offensive that I see that too?

Thread Thread
 
shameel profile image
Shameel Uddin

Checked your timeline, you spammed the same link to others as well.
Have fun doing this 🎉

Thread Thread
 
webjose profile image
José Pablo Ramírez Vargas • Edited

So let me see if I understand: You ask Chat GPT to create your articles, then try to hide the fact about it even against the site's guidelines, artificially upvote your posts, and when you are called upon all this you play the "offended" card and somehow I'm the bad guy. Is that it?

Check my timeline, no worries. Unlike some, I have nothing to hide.

Thread Thread
 
shameel profile image
Shameel Uddin

First of all, in this age of AI, if you don't use it then you're going to lack behind. I'm not going to give you any lecture on this anyway.

Furthermore, I'm the one making videos about it and what I'm doing here is, translating what I say in the video into the blogs in written format.

If you had made a little effort in finding this rather than just spamming the link into number of blogs including mine, you'd know that you made a mistake here :)

Lastly, I did not artificially "up vote" any of my blog. I share it with my close ones who show their support to me. Similar to couple of likes I get on YouTube.

Regardless, people like you are to be found everywhere and I've been told to learn to not to involve myself with the likes of you if I prepare myself to get into the world of content creation.

Thank you very much for the very first lesson. I'll be in your debt.

Thread Thread
 
webjose profile image
José Pablo Ramírez Vargas

It's good to hear you're learning. Hopefully, you have now read the guidelines: If your piece was created using AI entirely or in part, it must be tagged or the body of the article must disclose this fact. Furthermore, it cannot be used to promote goods or similar, and cannot be used to reply to comments. That's what it says in a nutshell. If you follow the guidelines, you have nothing to worry about, so I hope that the lesson you took here is: Follow the guidelines. Nobody is discouraging you from using AI.

I was not wrong when I posted the link: You are admitting the use of AI, so the guidelines apply. You made a big scene about it for reasons not yet disclosed, but we can agree to forget about that.

Thread Thread
 
shameel profile image
Shameel Uddin

"The lesson I learned was not to involve myself with the likes of you."

Thread Thread
 
webjose profile image
José Pablo Ramírez Vargas

Do as you wish, I suppose you live in a free country, as I do.

Collapse
 
cholasimmons profile image
Chola

I'm stunned to see this much trolling in 2024 let alone on dev.to. wow José just wow.
Learn when to put a plug in it.