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

本文标签: javascriptHow to perform nested routes in nestjsStack Overflow