router icon indicating copy to clipboard operation
router copied to clipboard

Wildcard Route Issue in Koa-Router v13

Open Caringor opened this issue 1 year ago • 1 comments

Environment

  • Package Name: koa-router

  • Version: v13.x (latest)

  • Node.js Version: v22.12.0

  • Operating System: macOS 14.4.1

Summary

After upgrading to koa-router v13 from v12, I encountered an issue with wildcard routes. Specifically, routes defined with * (e.g., :proxyPath*) no longer work as expected.

Steps to Reproduce

  1. Define a route using a wildcard parameter in the router setup:

    const Router = require('koa-router');
    const router = new Router();
    
    router.get(`/api/:token/:proxyPath*`, async (ctx) => {
        ctx.body = { message: 'Wildcard route works!' };
    });
    
  2. Start the server and send a request matching the route (e.g., /api/123/some/path/to/resource).

  3. Observe the error when running the application:

    TypeError: Missing parameter name at 49: https://git.new/pathToRegexpError
    

Expected Behavior

In koa-router v12 and earlier versions, the route defined with * correctly captured the remaining path segments. The server processed the request without errors.

Actual Behavior

With koa-router v13, the application throws the following error when using a wildcard route:

TypeError: Missing parameter name at 49: https://git.new/pathToRegexpError

Additional Information

  • This behavior change does not seem to be documented in the release notes.

  • I’ve reviewed the migration guide but couldn’t find details regarding this specific change or deprecation of wildcard support.

Questions

  1. Is this a bug introduced in v13, or was the behavior of wildcard parameters intentionally changed or removed?

  2. How can I achieve similar functionality (capturing all remaining path segments) in koa-router v13?

  3. If this is not supported in v13, is there an alternative approach or a recommended workaround?

Thank you for your assistance!

Caringor avatar Dec 29 '24 21:12 Caringor

I believe this is because of an issue with the path-to-regex import.

v13.0.1 (latest released, but doesn't show up on the releases page) imports "path-to-regexp": "^8.2.0". However, the master branch on this repo shows the same depency being imported as "path-to-regexp": "^6.3.0". The master branch is tagged with v13.1.0 but that version is not actually available in NPM.

I believe what happened is that the wrong code was accidentally released as v13.0.1. Releasing v13.1.0 as is should fix this issue.

ripecosta avatar Jan 16 '25 13:01 ripecosta

Is there a plan to release 13.1.0 from the releases page? It shows a september release date but still hasn't been published to NPM.

JonFranchi avatar Apr 01 '25 13:04 JonFranchi

Nevermind - this is now being released as @koa/router and I see it released: https://www.npmjs.com/package/@koa/router?activeTab=versions

This issue is resolved now if you switch from koa-router to @koa/router

JonFranchi avatar Apr 01 '25 14:04 JonFranchi