How to set a static redirection for a GET request in Nestjs?
Tags: resource
Published: 2022-08-13 19:05:40

---
  title: How to set a static redirection for a GET request in Nestjs?
  published: true
  tags: resource
  date: Sun Aug 14 2022 00:35:40 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/set-static-redirection-for-get-request-nestjs
  cover_image: https://melvingeorge.me/_next/static/images/main-6f6f51cd4e73037bcdb8f1b8362d0674.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/set-static-redirection-for-get-request-nestjs)
  
To set a static redirection for a `GET` request in Nestjs, we can use the `@Redirect()` decorator function from the `@nestjs/common` module and call it just above the `Controller` class's method that handles that `GET` request.

### TL;DR

```ts
// import `@Redirect()` decorator function from the `@nestjs/common` module
import { Controller, Get, Redirect } from "@nestjs/common";

// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetController {
  // 1. the @Get() decorator function will instruct Nestjs
  // that this is the default method that should be
  // invoked when the user requests a `GET` to `/greet` endpoint
  // 2. Using the @Redirect() decorator function to have static redirection
  // and passing the redirect url as the first argument and
  // the redirection status code as the second argument
  @Redirect("https://www.google.com/", 301)
  @Get()
  sayHello() {
    return `Hello World`;
  }
}
```

For example, let's say we have a `GET` request API endpoint called `/greet` and on request, it gives us the response of `Hello World`.

The code for the API endpoint will look like this,

```ts
import { Controller, Get } from "@nestjs/common";

// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetController {
  // the @Get() decorator function will instruct Nestjs
  // that this is the default method that should be
  // invoked when the user requests a `GET` to `/greet` endpoint
  @Get()
  sayHello() {
    return `Hello World`;
  }
}
```

Now, what if we need to statically redirect the `GET` requests to another URL? Let's say it to be `https://www.google.com/`.

To do that we can use the `@Redirect()` decorator function from the `@nestjs/common` module and call it just above the `Controller` class method that handles the `GET` request. In our case, it is the `sayHello()` method.

The `Redirect()` decorator function also accepts 2 arguments both of them being optional:

- the first argument is the URL we need it to redirect to. In our case, it is the `https://www.google.com/` URL.
- and the second argument is the redirection status code. In our case, it is `301` as the status code. Read more on [redirection status codes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#redirection_messages).

It can be done like this,

```ts
// import `@Redirect()` decorator function from the `@nestjs/common` module
import { Controller, Get, Redirect } from "@nestjs/common";

// the @Controller() decorator function will instruct Nestjs
// to add a route of `/greet`
@Controller("greet")
export class GreetController {
  // 1. the @Get() decorator function will instruct Nestjs
  // that this is the default method that should be
  // invoked when the user requests a `GET` to `/greet` endpoint
  // 2. Using the @Redirect() decorator function to have static redirection
  // and passing the redirect url as the first argument and
  // the redirection status code as the second argument
  @Redirect("https://www.google.com/", 301)
  @Get()
  sayHello() {
    return `Hello World`;
  }
}
```

We have successfully set a static redirection for a `GET` request in Nestjs. Yay 🥳!

See the above code live in [codesandbox](https://codesandbox.io/s/set-static-redirection-for-get-request-nestjs-q85sh3?file=/src/greet/greet.controller.ts).

You can also go to the [https://q85sh3.sse.codesandbox.io/greet](https://q85sh3.sse.codesandbox.io/greet) URL and see it getting redirected to the Google homepage.

That's all 😃.

### Feel free to share if you found this helpful 😃.

---