Nestjs 路由排除文件扩展名
大家好,这几天我遇到了一个
用 NestJS 构建的挑战 (https://nestjs.com/ )
我想要一条仅在没有文件扩展名时侦听的路由。
例如
localhost:3000 -> good
localhost:3000/ -> good
localhost:3000/test -> good
localhost:3000/test.txt -> ignore
localhost:3000/css/mycss.css -> ignore
,另外,当路线有效时,我想知道参数中的 slug
例如
localhost:3000/test
Get('/:slug')
params.slug = test
有人可以帮助我吗?
Hi for a couple of days I have this challenge
I'm building with NestJS (https://nestjs.com/)
I want to have a route that only listens when it doesn't have a file extension.
So for example
localhost:3000 -> good
localhost:3000/ -> good
localhost:3000/test -> good
localhost:3000/test.txt -> ignore
localhost:3000/css/mycss.css -> ignore
Also, when the route is valid, I want to know the slug in the params
for example
localhost:3000/test
Get('/:slug')
params.slug = test
Can somebody help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NestJs 不支持控制器路由中的 RegExp。它只接受字符串或字符串数组。因此,您无法在那里设置复杂的模式。
来源
但是,您可以获取请求值并手动检查。如果这种方式适合您,请尝试类似的操作
根据您的需要修改模式。
当前示例认为
something.txt
是不好的(任何包括点的东西都是不好的),但something
是一个好的模式。NestJs doesn't support RegExp in the controller routes. It only accepts strings or an array of strings. Thus, you cannot set sophisticated patterns there.
Source
However, you can get the request value and check it manually. If this way is okay for you then try something like this
Modify the pattern as you need.
The current example considers
something.txt
as bad (Anything including dot is bad) butsomething
as a good pattern.