Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware gets ctx.params for all routes #347

Open
zhuscat opened this issue Apr 7, 2017 · 3 comments
Open

Middleware gets ctx.params for all routes #347

zhuscat opened this issue Apr 7, 2017 · 3 comments

Comments

@zhuscat
Copy link

zhuscat commented Apr 7, 2017

I have a middleware(let's call it mw) which will use ctx.params to get the parameter in a path.

If I write:

router.use(mw)

The ctx.params in the mw is {}

But if I write:

router.use([
  '/example/:id',
  '/example2/:id',
], mw)

The ctx.params in the mw is { id: 'some_id' }

I want my middleware to be called for every route and I want to get the right ctx.params.

Is there any way to solve this problem?

@klerick
Copy link

klerick commented Apr 18, 2017

after init router
router.stack.forEach(routerItem => routerItem.stack.unshift(mw))

jpeddicord added a commit to jpeddicord/progcon that referenced this issue May 21, 2017
@mmazzarolo
Copy link

Does a cleaner way to do this exist?

@uinz
Copy link

uinz commented Oct 15, 2017

@zhuscat

const router = new Router({
  prefix: "/users/:id",
})

// GET /users/123
router.use("*", async (ctx, next) => {
  // ctx.params = {id: "123"}
  await next()
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants