admin管理员组文章数量:1415111
I have a controller (articles).
It performs routes: /articles
, /articles/:id
and this is it.
I need also the following routes - /articles/creator/:creatorId
, /articles/:id/like
, /articles/:id/unlike
, /articles/:id/ment
and so on.
Whether I need static path or action it is nested and its not working.
Partial solution for me - Controller (articles), Controller (articles/creator), Controller (articles/like), Controller (articles/unlike).
But this is a dumb solution and the concept of paths and actions is lost.
Is there an elegant solution to fix this? And how to achieve this in best way?
The code:
@Controller('articles')
class ArticlesController{
@Get(':articleId')
getById(@Param('articleId') articleId){}
@Post(':articleId/like)
like(@Param('articleId') articleId){}
@Get('creator/:creatorId')
getByCreator(@Param('creatorId') creatorId:string){}
}
I have a controller (articles).
It performs routes: /articles
, /articles/:id
and this is it.
I need also the following routes - /articles/creator/:creatorId
, /articles/:id/like
, /articles/:id/unlike
, /articles/:id/ment
and so on.
Whether I need static path or action it is nested and its not working.
Partial solution for me - Controller (articles), Controller (articles/creator), Controller (articles/like), Controller (articles/unlike).
But this is a dumb solution and the concept of paths and actions is lost.
Is there an elegant solution to fix this? And how to achieve this in best way?
The code:
@Controller('articles')
class ArticlesController{
@Get(':articleId')
getById(@Param('articleId') articleId){}
@Post(':articleId/like)
like(@Param('articleId') articleId){}
@Get('creator/:creatorId')
getByCreator(@Param('creatorId') creatorId:string){}
}
Share
Improve this question
edited Apr 16, 2019 at 22:05
Alexander Kolarov
asked Apr 16, 2019 at 21:19
Alexander KolarovAlexander Kolarov
8551 gold badge12 silver badges24 bronze badges
1
- 1 Please also include the relevant parts of your code in your question. (In this case your controller.) It makes answering your question easier. :-) – Kim Kern Commented Apr 16, 2019 at 21:41
1 Answer
Reset to default 6Just as you have a dynamic route parameter for an article id, you can have one for an action as well:
@Controller('articles')
export class ArticlesController {
@Get(':id/:action')
findAll(@Param('id') id, @Param('action') action) {
return `You chose ${action} for article ${id}`;
}
本文标签: javascriptHow to perform nested routes in nestjsStack Overflow
版权声明:本文标题:javascript - How to perform nested routes in nestjs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745221363a2648409.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论