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

Created on 12 Oct 2016  路  2Comments  路  Source: expressjs/express

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.

4.x question

Most helpful comment

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.

All 2 comments

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.

Thanks @dougwilson for the clarification.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AndrewEQ picture AndrewEQ  路  4Comments

snowdream picture snowdream  路  3Comments

wxs77577 picture wxs77577  路  3Comments

ZeddYu picture ZeddYu  路  3Comments

guyisra picture guyisra  路  3Comments