Home > Blockchain >  Express Router with nested params doesn't work
Express Router with nested params doesn't work

Time:07-19

I have a working example that matches the desired route path, but I have to recreate the full file name in the controller from the param 'cameraname' and the knowing the rest of the filename. Here's the code that works like that:

 this.router.get(
      `${this.path}/watch/:cameraname([a-zA-Z\_] ).m3u8`,
      [authMiddleware()],
      this.camerasController.getHLSPlaylist,
    )

I wish to modify this with the addition of a parameter that includes the filename extension like this:

 this.router.get(
      `${this.path}/watch/:filename(:cameraname([a-zA-Z\_] ).m3u8)`,
      [authMiddleware()],
      this.camerasController.getHLSPlaylist,
    )

But that doesn't work (no matches). Here's an example of the request path:

/cameras/watch/Garage_Door.m3u8

This first code works, but not the second. There are more routes similar to this that are more complex with more parameters. This case is the simplest. What am I doing wrong?

CodePudding user response:

The express documentation does not mention the possiblity of nested parameters.

But

this.router.get(
  `${this.path}/watch/:cameraname([a-zA-Z\_] ).:suffix`,
  ...)

works and gives you the suffix (that is, the rest of the filename) as req.params.suffix.

Does that cover your requirement?

  • Related