2

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/comment 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){}

}
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. :-) Commented Apr 16, 2019 at 21:41

1 Answer 1

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}`;
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.