新手入门NestJS(八)- 控制器重定向、路由参数

重定向

Nest.js提供了一种方式可以重定向路由

方式可以通过使用装饰器@Redirect和res.redirect

这里记录下如何使用装饰器来重定向

1
2
3
4
5
6
7
8
9
@Get('items')
@Redirect('https://www.gowhich.com', 302)
getItems(@Query('version') version) {
if (version && version == 5) {
return {
url: 'https://www.gowhich.com/cats/items/v5',
};
}
}

当访问http://127.0.0.1:3000/cats/items,URI会被重定向到https://www.gowhich.com

当访问http://127.0.0.1:3000/cats/items?version=5,URI会被重定向到https://www.gowhich.com/cats/items/v5

获取路由参数

这里说的路由参数值的是,当访问cats/1这样的路由的时候,能否获取到1这个参数值

当然Nest.js也提供了一个非常好用的装饰器@Param

这个装饰器,我们可以使用两种方式来获取到参数

第一种

1
2
3
4
@Get(':id')
getOne(@Param() param): string {
return `This action return id #${param.id}`;
}

第二种

1
2
3
4
@Get(':name')
getName(@Param('name') name): string {
return `This action return name #${name}`;
}