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
// 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,
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.
It can be done like this,
// 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.
You can also go to the https://q85sh3.sse.codesandbox.io/greet URL and see it getting redirected to the Google homepage.
That's all 😃.