Skip to content

Older version of path-to-regexp in Express package.json causing problems with some paths #4741

Closed
@jfriend00

Description

@jfriend00

Running Express 4.17.1, if you define a route like this:

app.get("/(user|u)", ...)

with the intention of matching either /user or /u, then you get this error when the app.get() first executes to register the route:

  return new RegExp(path, flags);
         ^

SyntaxError: Invalid regular expression: /^\/(?(?:([^\/]+?))|u)\/?$/: Invalid group
    at new RegExp (<anonymous>)
    at pathtoRegexp (D:\code\test\temp\node_modules\express\node_modules\path-to-regexp\index.js:128:10)
    at new Layer (D:\code\test\temp\node_modules\express\lib\router\layer.js:45:17)
    at Function.route (D:\code\test\temp\node_modules\express\lib\router\index.js:494:15)
    at Function.app.<computed> [as get] (D:\code\test\temp\node_modules\express\lib\application.js:481:30)
    at Object.<anonymous> (D:\code\test\temp\temp.js:45:5)

The issue occurs in the path-to-regexp module. But, it appears to be something that has been fixed in more recent versions of path-to-regexp. Express 4.17.1 has path-to-regexp: 0.1.7 in its package.json, whereas the latest version of path-to-regexp is 6.2.0. And, when I manually call path-to-regexp("/(user|u)") in this latest version, it does not cause the problem seen above. In fact, the code that causes the problem above has been completely rewritten in path-to-regexp as it looks nothing like the older version I step through in Express.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions