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

how to make wrapper for (nested) router? #267

Closed
dimonnwc3 opened this issue Apr 23, 2016 · 2 comments
Closed

how to make wrapper for (nested) router? #267

dimonnwc3 opened this issue Apr 23, 2016 · 2 comments

Comments

@dimonnwc3
Copy link

i want to wrap nested router:

let router = new Router({prefix: 'users'});
router = router
  .get('/', userCtrl.getUsers)
  .post('/', userCtrl.post)
  .get('/:id', userCtrl.get)
  .patch('/:id', userCtrl.patch)
  .delete('/:id',  userCtrl.del)
  .routes();

function* wrapper(next) {
   console.log('enter');
    //my wrapper code
    try {
      console.log('run');
      yield* router.call(this, next);
    } finally {
      console.log('exit');
    }
}

let mainRouter = new Router();
mainRouter.use(wrapper);

But this does not work.

I get to debug:

  koa-router defined route HEAD,GET /users/ +41ms
  koa-router defined route POST /users/ +1ms
  koa-router defined route HEAD,GET /users/:id +0ms
  koa-router defined route PATCH /users/:id +1ms
  koa-router defined route DELETE /users/:id +0ms
  koa-router defined route  (.*) +0ms

and then when request

  koa-router GET /users +3s
  koa-router test (.*) /^(.*)(?:\/(?=$))?(?=\/|$)/i +0ms
  koa-router test (.*) /^(.*)(?:\/(?=$))?(?=\/|$)/i +0ms
  --> GET /users 404 50ms 1.31kb

I have also tried: dispatch.router = router.router;
And it's worked, but without my wrapper.

This is a simplified example, in my application, I do a lot of routers and do one wrapper for each, with different parameters inside.

@smcmurray
Copy link

I think this may be related to my comment on issue #257

@YanLobat
Copy link

Is it still an issue? @dimonnwc3

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