javascript - How to perform nested routes in nestjs? - Stack Overflow

I have a controller (articles).It performs routes: articles, articles:idand this is it.I need also

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
Add a ment  | 

1 Answer 1

Reset to default 6

Just 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}`;
  }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745221363a4617263.html

相关推荐

  • javascript - How to perform nested routes in nestjs? - Stack Overflow

    I have a controller (articles).It performs routes: articles, articles:idand this is it.I need also

    1小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信