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

SyntaxError: Invalid regular expression during path-to-regexp conversion #868

Closed
XanderEndre opened this issue May 23, 2023 · 4 comments · May be fixed by #877
Closed

SyntaxError: Invalid regular expression during path-to-regexp conversion #868

XanderEndre opened this issue May 23, 2023 · 4 comments · May be fixed by #877

Comments

@XanderEndre
Copy link

XanderEndre commented May 23, 2023

Hi there,

I've encountered an issue when using express-openapi with my Express.js application. This application was based on a fresh installation.

The error I'm facing is as follows:

C:\Users\Alex\Visual Studio Code\TestProject\CustomerService\api\node_modules\express\node_modules\path-to-regexp\index.js:128
  return new RegExp(path, flags);
         ^

SyntaxError: Invalid regular expression: /^\/customer\(?:([^\/]+?))\/?$/: Unmatched ')'

This error occurs when my application is starting up.

Here is my project structure:

C:.
│   app.js
│   package-lock.json
│   package.json
│
├───api
│   │   api-doc.js
│   │
│   └───paths
│       │   test.js
│       │
│       └───customer
│               customer.js
│               {id}.js
│
└───bin
        www

Here is my package.json

{
  "name": "customer-service",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "node": "nodemon app.js",
    "start": "node ./bin/www"
  },
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "debug": "~4.3.4",
    "dotenv": "^16.0.3",
    "express": "~4.18.2",
    "express-openapi": "^12.1.1",
    "morgan": "~1.10.0",
    "nodemon": "^2.0.20",
    "swagger-ui-express": "^4.6.1"
  }
}

Here is my code for the test.js, customer.js, and customer/{id}.js

module.exports = function () {
    let operations = {
        GET,
    };

    async function GET(req, res, next) {
        console.log("IN GET")
        console.log("REQUEST:", req)
        try {
            res.status(200).send();
        } catch (err) {
            console.error(err);
            res.status(500).json({
                error: "Internal Server Error"
            });
        }
    }

    GET.apiDoc = {
        summary: "Retrieve data from the api",
        operationId: "get-test",
        responses: {
            200: {
                description: "Returns if it was able to connect to the service",
            },
            500: {
                description: "Internal Server Error",
                content: {
                    "application/json": {
                        schema: {
                            $ref: "#/components/schemas/Error",
                        },
                    },
                },
            },
        },
    };
    return operations;
};

I am following the guidelines mentioned in the express-openapi documentation to structure my path files and use the {id} syntax for URL parameters.

Interestingly, I can access the /test route without any issues. However, I am not able to access the /customer by itself. I receive a 404 error. When adding the /customer/{id} file I receive the error detailed above.

The error seems to be related to the path-to-regexp module, but I'm not directly using this module in my code. It's being used internally by express-openapi.

I suspect the issue is due to the {id} file name, which could be getting passed as a part of the path to the path-to-regexp function.

If you could help me understand what's causing this error and how I could resolve it, I would greatly appreciate it. Please let me know if you need any further information. If you know better ways to debug this code or get a better error message, please let me know.

Thank you for your time and assistance!

@XanderEndre
Copy link
Author

Opened PR #869 to resolve this issue.

@renkei
Copy link

renkei commented Jun 2, 2023

Opened another PR #877 that modifies fs-routes instead and fixes this issue and also #871

@vJan00
Copy link

vJan00 commented Aug 13, 2023

Still a problem over here - any workarounds yet?

@anthonyloukinas
Copy link

anthonyloukinas commented Jan 30, 2024

I'm also still seeing this issue:

\node_modules\path-to-regexp\index.js:128
  return new RegExp(path, flags);
         ^
SyntaxError: Invalid regular expression: /^\/api\v1\users\(?:([^\/]+?))\/?$/: Unmatched ')'
    at new RegExp (<anonymous>)
    at pathtoRegexp (D:\Projects\REDACTED\REDACTED-api\node_modules\path-to-regexp\index.js:128:10)
    at new Layer (D:\Projects\REDACTED\REDACTED-api\node_modules\express\lib\router\layer.js:45:17)
    at Function.route (D:\Projects\REDACTED\REDACTED-api\node_modules\express\lib\router\index.js:505:15)
    at Function.app.<computed> [as get] (D:\Projects\REDACTED\REDACTED-api\node_modules\express\lib\application.js:498:30)
    at Object.visitOperation (D:\Projects\REDACTED\REDACTED-api\node_modules\express-openapi\index.ts:205:25)
    at D:\Projects\REDACTED\REDACTED-api\node_modules\openapi-framework\index.ts:605:19
    at Set.forEach (<anonymous>)
    at D:\Projects\REDACTED\REDACTED-api\node_modules\openapi-framework\index.ts:364:9
    at Array.forEach (<anonymous>)

I also have a similar structure where I'm using {id}.js. This issue only happens on Windows, my Mac/Linux workstations don't have these issues.

Package.json

"express-openapi": "^12.1.3"

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

Successfully merging a pull request may close this issue.

4 participants