Backend error #25

Closed
opened 2026-10-14 16:21:35 +00:00 by Syndria98 · 7 comments
Owner

The backend does not start it gives this error:

[BACKEND] ReferenceError: foundryCorsPaths is not defined
[BACKEND]     at /home/arans/Pro/dragonroll/backend/src/index.js:65:3
[BACKEND]     at Layer.handleRequest (/home/arans/Pro/dragonroll/backend/node_modules/router/lib/layer.js:152:17)
[BACKEND]     at trimPrefix (/home/arans/Pro/dragonroll/backend/node_modules/router/index.js:342:13)
[BACKEND]     at /home/arans/Pro/dragonroll/backend/node_modules/router/index.js:297:9
[BACKEND]     at processParams (/home/arans/Pro/dragonroll/backend/node_modules/router/index.js:582:12)
[BACKEND]     at next (/home/arans/Pro/dragonroll/backend/node_modules/router/index.js:291:5)
[BACKEND]     at cookieParser (/home/arans/Pro/dragonroll/backend/node_modules/cookie-parser/index.js:57:14)
[BACKEND]     at Layer.handleRequest (/home/arans/Pro/dragonroll/backend/node_modules/router/lib/layer.js:152:17)
[BACKEND]     at trimPrefix (/home/arans/Pro/dragonroll/backend/node_modules/router/index.js:342:13)
[BACKEND]     at /home/arans/Pro/dragonroll/backend/node_modules/router/index.js:297:9

The backend does not start it gives this error: ``` [BACKEND] ReferenceError: foundryCorsPaths is not defined [BACKEND] at /home/arans/Pro/dragonroll/backend/src/index.js:65:3 [BACKEND] at Layer.handleRequest (/home/arans/Pro/dragonroll/backend/node_modules/router/lib/layer.js:152:17) [BACKEND] at trimPrefix (/home/arans/Pro/dragonroll/backend/node_modules/router/index.js:342:13) [BACKEND] at /home/arans/Pro/dragonroll/backend/node_modules/router/index.js:297:9 [BACKEND] at processParams (/home/arans/Pro/dragonroll/backend/node_modules/router/index.js:582:12) [BACKEND] at next (/home/arans/Pro/dragonroll/backend/node_modules/router/index.js:291:5) [BACKEND] at cookieParser (/home/arans/Pro/dragonroll/backend/node_modules/cookie-parser/index.js:57:14) [BACKEND] at Layer.handleRequest (/home/arans/Pro/dragonroll/backend/node_modules/router/lib/layer.js:152:17) [BACKEND] at trimPrefix (/home/arans/Pro/dragonroll/backend/node_modules/router/index.js:342:13) [BACKEND] at /home/arans/Pro/dragonroll/backend/node_modules/router/index.js:297:9 ```
Syndria98 added the ai-ready label 2026-10-14 16:21:35 +00:00
Agent added the working label 2026-10-14 16:21:37 +00:00
Collaborator

The error is at line 65: foundryCorsPaths is undefined. Let me check the git history to understand how this happened.

The error is at line 65: `foundryCorsPaths` is undefined. Let me check the git history to understand how this happened.
Collaborator

Now let me verify the fix — syntax check, confirm no other stale references exist, and do a quick functional smoke test of the middleware logic:

Now let me verify the fix — syntax check, confirm no other stale references exist, and do a quick functional smoke test of the middleware logic:
Collaborator

No stale references remain. Let me smoke-test the actual middleware by booting the server and hitting routes — checking whether a missing Mongo would block it first:

No stale references remain. Let me smoke-test the actual middleware by booting the server and hitting routes — checking whether a missing Mongo would block it first:
Collaborator

connectDB exits on failure, but app.listen runs immediately and mongoose takes ~30s to time out — enough of a window to curl. Let me check for a local mongod first, then boot and test:

`connectDB` exits on failure, but `app.listen` runs immediately and mongoose takes ~30s to time out — enough of a window to curl. Let me check for a local mongod first, then boot and test:
Collaborator

Dependencies installed. Now boot the backend and exercise the failing request path:

Dependencies installed. Now boot the backend and exercise the failing request path:
Collaborator

Fixed. The backend crash was a leftover variable name from a refactor in backend/src/index.js.

Root cause: The commit that introduced the shared open-CORS handling for service link/sync routes defined the path list as openCorsPaths (line 46) and applied openCors to it, but the guard middleware that skips the restrictive app-wide CORS for those same paths still referenced the old name foundryCorsPaths (line 65). That name was never defined, so every request passing through the middleware threw ReferenceError: foundryCorsPaths is not defined → HTTP 500 — matching the reported stack trace (index.js:65:3 → Layer.handleRequest → … → cookieParser).

Fix: one-line rename to the defined variable:

   if (foundryCorsPaths.some((path) => req.path === path || req.path.startsWith(`${path}/`))) return next();
+  if (openCorsPaths.some((path) => req.path === path || req.path.startsWith(`${path}/`))) return next();

Verification (ran the real server before and after):

  • Before the fix: GET /api/test returned HTTP 500 with the exact stack trace from the issue.
  • After the fix: the request passes the middleware cleanly (401 from the JWT guard, which is the endpoint's normal unauthenticated behavior).
  • OPTIONS preflight on /api/foundry/sync/* from a foreign origin gets the open CORS headers (Access-Control-Allow-Origin: <origin>, Authorization + bot-secret header allowed) and is not overwritten by the restrictive CORS — the guard's whole purpose.
  • Preflight on app routes (/api/campaign/list) still goes through appCors as before.
  • grep confirms no other stale foundryCorsPaths references remain anywhere in the repo.
  • npm test: 34/34 pass (existing suite covers the Foundry module sync logic; the repo has no backend HTTP test harness, so verification was done by booting the actual server rather than adding unrelated infrastructure for a one-line rename).
Fixed. The backend crash was a leftover variable name from a refactor in `backend/src/index.js`. **Root cause:** The commit that introduced the shared open-CORS handling for service link/sync routes defined the path list as `openCorsPaths` (line 46) and applied `openCors` to it, but the guard middleware that skips the restrictive app-wide CORS for those same paths still referenced the old name `foundryCorsPaths` (line 65). That name was never defined, so **every** request passing through the middleware threw `ReferenceError: foundryCorsPaths is not defined` → HTTP 500 — matching the reported stack trace (`index.js:65:3` → `Layer.handleRequest` → … → `cookieParser`). **Fix:** one-line rename to the defined variable: ```diff if (foundryCorsPaths.some((path) => req.path === path || req.path.startsWith(`${path}/`))) return next(); + if (openCorsPaths.some((path) => req.path === path || req.path.startsWith(`${path}/`))) return next(); ``` **Verification (ran the real server before and after):** - Before the fix: `GET /api/test` returned HTTP 500 with the exact stack trace from the issue. - After the fix: the request passes the middleware cleanly (401 from the JWT guard, which is the endpoint's normal unauthenticated behavior). - `OPTIONS` preflight on `/api/foundry/sync/*` from a foreign origin gets the open CORS headers (`Access-Control-Allow-Origin: <origin>`, `Authorization` + bot-secret header allowed) and is **not** overwritten by the restrictive CORS — the guard's whole purpose. - Preflight on app routes (`/api/campaign/list`) still goes through `appCors` as before. - `grep` confirms no other stale `foundryCorsPaths` references remain anywhere in the repo. - `npm test`: 34/34 pass (existing suite covers the Foundry module sync logic; the repo has no backend HTTP test harness, so verification was done by booting the actual server rather than adding unrelated infrastructure for a one-line rename).
Collaborator

Implementation is ready for review: #26

Implementation is ready for review: https://git.aranroig.com/Syndria98/dragonroll/pulls/26
Agent added help wanted and removed working labels 2026-10-14 16:24:43 +00:00
Agent referenced this issue from a commit 2026-10-14 16:24:44 +00:00
Sign in to join this conversation.
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Syndria98/dragonroll#25