seanpmaxwell/overnight

If a controller has a param it's not send to the child routes

Closed this issue · 3 comments

@Controller('api/cache/:env')
export class CacheController {
  @Get('')
  public async get(req: Request, res: Response) {
    req.params.env // this is undefined
  }

You need use the @ClassOptions({mergeParams: true}) option. That is a requirement for working with nested express routes. Here's an example:
https://github.com/seanpmaxwell/overnight/blob/master/sample-project/src/controllers/nesting/b-children/ChildB1Controller.ts

I saw that and adding it worked. But as I'm not using nested routes it seems a bit surprising as a new user coming to the api. I would think the controller's method would always have access to the params of the controller.

I personally never used that option either and only learned about it from others. Under the hood Overnight does merge nested routes. The controller uses a express.Router() object and the methods are done with router.get() router.post() etc. So in a way you could think of it as nesting routes the same way you would with express.