[英]@Get('*') doesn't match empty (NestJS)
Within my Nest backend I'm trying to match a url within my controller在我的 Nest 后端中,我试图匹配控制器中的 url
@Controller('admin')
export class AdminController {
@Get('*')
getAdminB(@Res() response): void {
response.sendFile(path.resolve('./public/admin/index.html'));
}
}
This should match the following URLS:这应该与以下 URL 匹配:
/admin
/admin/
/admin/anything
However, the above get @Get('*')
doesn't match /admin
.但是,上面的 get
@Get('*')
与/admin
不匹配。 Should I add an other route with @Get()
or is there a fix for this?我应该用
@Get()
添加其他路线还是有解决办法?
I need this because I have to serve an angular app from /admin
我需要这个,因为我必须从
/admin
提供一个有角度的应用程序
You can use the path @Get('/?*')
to match all routes.您可以使用路径
@Get('/?*')
来匹配所有路由。
Nest uses the util function validatePath()
to build the path. Nest 使用 util 函数
validatePath()
来构建路径。 When you have a prefix ( admin
in your case), then nest will always add a /
between the prefix and the path from your route decorator unless the first char is already a /
.当你有一个前缀(
admin
你的情况),然后窝总会添加/
从你的路线装饰前缀和路径之间,除非第一个字符已经是一个/
。
export const validatePath = (path?: string): string =>
path
? path.charAt(0) !== '/' ? '/' + path : path
: '';
So the path that is given to express will be admin/?*
which matches any path that starts with admin
.因此,提供给 express 的路径将是
admin/?*
,它匹配以admin
开头的任何路径。 Careful , this also includes eg adminarea/1
!小心,这也包括例如
adminarea/1
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.