Used app / router does not exec `params` middleware without call from within app / router

I was just creating a router with only a param in it and found out the hard way that it doesn’t execute in the app it’s app.use in.

Here’s the code:

function myRouter () {
  const router = Router({mergeParams: true})
  router.param('appId', async (req, res, next, appId) => {
    console.log('hi')
    return next()
  })
  router.all('/:appId/*', (req, res, next) => {
    return next()
  })
  return router
}

This will not log hi without.

  router.all('/:appId/*', (req, res, next) => {
    return next()
  })

Which seems a bit unintuitive.

Author: Fantashit

1 thought on “Used app / router does not exec `params` middleware without call from within app / router

  1. Right, the router.params are confined to the router they were declared in. This is one of the purposes for creating new routers rather than reusing existing routers: because you want to create your own parameters. This allows for each router to have it’s own parameter scope and allows for composability by not having routers interfere with each other.

Comments are closed.