SyntaxError: Invalid regular expression during path-to-regexp conversion
XanderEndre opened this issue · comments
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!
Opened PR #869 to resolve this issue.
Still a problem over here - any workarounds yet?
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"