2

How I want to apply middleware to all paths in koa-route, e.g.

router1.use( (ctx) => {
    console.error("hello 0 ...");
    console.log(ctx.url);
}
router1.all( (ctx) => {
    console.error("hello 0 ...");
    console.log(ctx.url);
}

It comes back like this path += str.slice(index, offset) ^

TypeError: str.slice is not a function Any hints ? Thanks!

user3552178
  • 2,719
  • 8
  • 40
  • 67
  • I just answered this question yesterday. Look there. – Evert Sep 14 '17 at 03:48
  • Possible duplicate of [Koa2: how to write chain of middleware?](https://stackoverflow.com/questions/46187902/koa2-how-to-write-chain-of-middleware) – Evert Sep 14 '17 at 03:48
  • @Evert think it's a little bit different, when I'm using koa-router, I don't want to go back use "app.", want to stay with "route.". Actually I hope I found a way, see my answer please. Let's discuss, :) – user3552178 Sep 14 '17 at 14:04
  • You should be fine with using app for this. koa-router is just another middleware. Your workaround might work, but it's a hack – Evert Sep 14 '17 at 18:14

1 Answers1

0

I hope this is it,

router1.use(/(.*)/, (ctx) => {
    console.error("hello 0 ...");
    console.log(ctx.url);
}
user3552178
  • 2,719
  • 8
  • 40
  • 67