routing-controllers icon indicating copy to clipboard operation
routing-controllers copied to clipboard

fix: create controllers in specified order

Open getfatday opened this issue 2 years ago • 2 comments

Description

Controllers are created in the order they are registered with metadata, not in the order they are passed into the router controller options.

@Controller()
class ThirdController {
  @Get('/*')
  getAll() {
    return 'Third';
  }
}

@Controller()
class SecondController {
  @Get('/second/*')
  getAll() {
    return 'Second';
  }
}

@Controller()
class FirstController {
  @Get('/second/first/*')
  getAll() {
    return 'First';
  }
}

createExpressServer({
  controllers: [FirstController, SecondController, ThirdController],
})

Expected behavior

Requests are handle by the First controller, which is the first in the controller order, but last to be registered.

GET /second/first/any

First

Actual behavior

Requests are handle by the Third controller, which is the last in the controller order, but first to be registered.

GET /second/first/any

Third

getfatday avatar Sep 16 '21 19:09 getfatday

@getfatday Have you found any workarounds? Or are you using your fork with a fix?

vesurbag avatar Oct 13 '21 11:10 vesurbag

@vesurbag, the workaround is to ensure your imports are in the correct order. Not ideal for larger projects.

getfatday avatar Oct 13 '21 15:10 getfatday