Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

question: conflicting routes #213

Closed
codedoge opened this issue Jul 17, 2017 · 6 comments · Fixed by #568
Closed

question: conflicting routes #213

codedoge opened this issue Jul 17, 2017 · 6 comments · Fixed by #568
Labels
type: question Questions about the usage of the library.
Milestone

Comments

@codedoge
Copy link

codedoge commented Jul 17, 2017

Hi,
If I build a controller like this:

@JsonController()
export class ItemController {

    @Get("/items/:itemId")
    getItem(@Param("itemId") itemId: number) {
        log.info("/items/:itemId")
        // if (itemId == "all")return this.getAllItems();
        // return new ItemModel({ite_id: itemId}).fetch().then(marshalBookshelf);
        return {message: "/items/:itemId"};
    }

    @Get(new RegExp("/items/all"))
    getAllItems() {
        log.info("/items/all")
        return {message: "/items/all"};
    }

}

and I try to GET "/items/all", I notice that both methods are invoked.
I know that this is not a good way to implement a REST api, in fact I doscourage this, however I'm shifting from a legacy project and I would like to move to routing-controllers. I noticed that by using the native express way to declare routes (like in the legacy project), the route being selected when invoking "/items/all" is only one ("/items/all"), so there are no multiple route handlers being executed.

@NoNameProvided
Copy link
Member

NoNameProvided commented Jul 17, 2017

Note: please use three backtick (`) to format your multiline code and append the language type after the opening backticks. (This time it's ts)

Routing controllers will match both routes because your items/:itemId route will match alphanumeric characters and all is alpanumeric. What you want is to change it to @Get('items/:itemId([0-9]+)') to explicitly expect numerical id-s.

So it would looks like:

@JsonController()
export class ItemController {

  @Get('items/:itemId([0-9]+)')
  getItem(@Param("itemId") itemId: number) {
      log.info("/items/:itemId")
      return {message: "/items/:itemId"};
  }

  @Get('/items/all')
  getAllItems() {
      log.info("/items/all")
      return {message: "/items/all"};
  }
}

@codedoge
Copy link
Author

codedoge commented Jul 17, 2017

Thank you for the solution and the formatting tip, it solved my problem.
I didn't know it was possible to apply regex filters like this.

@mcwebb
Copy link

mcwebb commented Apr 19, 2018

Can I suggest that @NoNameProvided's comment be added to the docs, until I saw this it wasn't clear to me how to use a regex in routing params.

@ElegantSoft
Copy link

I have mongodb and this regex will not be helpful for me so what should I do

@PeckZeg
Copy link

PeckZeg commented Apr 24, 2020

I have mongodb and this regex will not be helpful for me so what should I do

i use this pattern

@JsonController()
export class ItemController {

    @Get("/items/:itemId([0-9a-fA-F]{24})")
    getItem(@Param("itemId") itemId: number) {
        log.info("/items/:itemId")
        // if (itemId == "all")return this.getAllItems();
        // return new ItemModel({ite_id: itemId}).fetch().then(marshalBookshelf);
        return {message: "/items/:itemId"};
    }

    @Get(new RegExp("/items/all"))
    getAllItems() {
        log.info("/items/all")
        return {message: "/items/all"};
    }

}

@jotamorais jotamorais linked a pull request May 29, 2020 that will close this issue
@jotamorais jotamorais added this to the 0.9.x release milestone May 29, 2020
@NoNameProvided NoNameProvided changed the title Conflicting routes question: conflicting routes Aug 9, 2020
@NoNameProvided NoNameProvided added the type: question Questions about the usage of the library. label Aug 9, 2020
@typestack typestack locked as resolved and limited conversation to collaborators Aug 9, 2020
@NoNameProvided
Copy link
Member

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: question Questions about the usage of the library.
Development

Successfully merging a pull request may close this issue.

6 participants