Match last URL param including slashes in Koa router

290 Views Asked by At

I use koa-router, and would like to match part of the URL (potentially) including slashes. For instance, everything that matches /foo/xxx, /foo/yyy, /foo/dir/xxx, and /foo/a/b/c/d.

Something like the following, if *path meant the same as ":path but including slashes":

router.get('/foo/*path', async (ctx) => {
    console.log(`PATH: ${ctx.params.path}`);
});

Being able to say "catch everything starting with /foo/" would work as well.

I am stuck here, I don't find any way for koa-router to allow me to do this.

1

There are 1 best solutions below

1
Rubek Joshi On BEST ANSWER

I found the answer in one of koa's github issue. You need to simply create your router as follows:

router.get('/foo/:splat*', async (ctx) => {
  // do what you want
});