# Redirect Decorator
Module | import { Redirect } from "@tsed/common" |
---|---|
Source | /packages/common/src/mvc/decorators/method/redirect.ts |
# Overview
function Redirect(url: string): Function;
export function Redirect(status: number, url: string): Function;
# Description
Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an HTTP status code . If not specified, status defaults to “302 “Found”.
@Redirect('/foo/bar')
@Redirect(301, 'http://example.com')
private myMethod() {}
2
3
Redirects can be a fully-qualified URL for redirecting to a different site:
@Redirect('http://google.com');
private myMethod() {}
2
Redirects can be relative to the root of the host name. For example, if the application is on http://example.com/admin/post/new, the following would redirect to the URL http://example.com/admin:
@Redirect('/admin');
Redirects can be relative to the current URL. For example, from http://example.com/blog/admin/ (notice the trailing slash), the following would redirect to the URL http://example.com/blog/admin/post/new.
@Redirect('post/new');
Redirecting to post/new from http://example.com/blog/admin (no trailing slash), will redirect to http://example.com/blog/post/new.
If you found the above behavior confusing, think of path segments as directories (with trailing slashes) and files, it will start to make sense.
Path-relative redirects are also possible. If you were on http://example.com/admin/post/new, the following would redirect to http//example.com/admin/post:
@Redirect('..');
``
A back redirection redirects the request back to the referer, defaulting to / when the referer is missing.
```typescript
@Redirect('back');
2
3
4
5
6
7
@param url @decorator @operation @response @headers
- Session & cookies
- Passport.js
- TypeORM
- Mongoose
- GraphQL
- Socket.io
- Swagger
- AJV
- Multer
- Serve static files
- Templating
- Throw HTTP Exceptions
- Customize 404
- AWS
- Jest
- Seq
- Controllers
- Providers
- Model
- Converters
- Middlewares
- Pipes
- Interceptors
- Authentication
- Hooks
- Injection scopes
- Custom providers
- Custom endpoint decorator
- Testing