How to use query parameters in Nest.js?
Query parameters
You have to remove :params
for it to work as expected:
@Get('findByFilter')async findByFilter(@Query() query): Promise<Article[]> { // ...}
Path parameters
The :param
syntax is for path parameters and matches any string on a path:
@Get('products/:id')getProduct(@Param('id') id) {
matches the routes
localhost:3000/products/1localhost:3000/products/2abc// ...
Route wildcards
To match multiple endpoints to the same method you can use route wildcards:
@Get('other|te*st')
will match
localhost:3000/otherlocalhost:3000/testlocalhost:3000/te123st// ...
If you have you parameter as part or url: /articles/${articleId}/details, you wold use @Param
@Get('/articles/:ARTICLE_ID/details')async getDetails( @Param('ARTICLE_ID') articleId: string)
IF you want to provide query params /article/findByFilter/bug?google=1&baidu=2, you could use
@Get('/article/findByFilter/bug?')async find( @Query('google') google: number, @Query('baidu') baidu: number,)
we can use @Req()
@Get(':framework')getData(@Req() request: Request): Object { return {...request.params, ...request.query};}
/nest?version=7
{ "framework": "nest", "version": "7"}