Skip to content

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

@XanderEndre

Description

@XanderEndre

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions