如何在Nest.js中使用查询参数? [英] How to use query parameters in Nest.js?
问题描述
我是Nest.js的新生.
I am a freshman in Nest.js.
我的代码如下
@Get('findByFilter/:params')
async findByFilter(@Query() query): Promise<Article[]> {
}
我已经使用postman
来测试此路由器
I have used postman
to test this router
http://localhost:3000/article/findByFilter/bug? google = 1& baidu = 2
实际上,我可以获得查询结果{ google: '1', baidu: '2' }
.但是我不清楚为什么URL有一个字符串'bug'
?
Actually, I can get the query result { google: '1', baidu: '2' }
. But I'm not clear why the url has a string 'bug'
?
如果我像删除该单词一样
If I delete that word just like
http://localhost:3000/article/findByFilter?google = 1& bai == 2
然后邮递员将显示statusCode 404
.
then the postman will shows statusCode 404
.
实际上,我不需要单词bug
,就像http://localhost:3000/article/findByFilter?google=1&baidu=2
Actually, I don't need the word bug
, how to custom the router to realize my destination just like http://localhost:3000/article/findByFilter?google=1&baidu=2
这是另一个问题,如何使多个路由器指向一种方法?
Here's another question is how to make mutiple router point to one method?
推荐答案
您必须删除:params
才能使其按预期工作:
You have to remove :params
for it to work as expected:
@Get('findByFilter')
async findByFilter(@Query() query): Promise<Article[]> {
// ...
}
:param
语法用于路径参数,并且匹配路径上的任何字符串:
The :param
syntax is for path parameters and matches any string on a path:
@Get('products/:id')
getProduct(@Param('id') id) {
匹配路线
localhost:3000/products/1
localhost:3000/products/2abc
// ...
路由通配符
要将多个端点与相同的方法进行匹配,可以使用路由通配符:
Route wildcards
To match multiple endpoints to the same method you can use route wildcards:
@Get('other|te*st')
将匹配
localhost:3000/other
localhost:3000/test
localhost:3000/te123st
// ...
这篇关于如何在Nest.js中使用查询参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!