Return JSON 404 for unknown API routes#14
Merged
ChrisAdamsdevelopment merged 1 commit intoMay 4, 2026
Merged
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideAdds a JSON 404 catch-all handler for unknown /api routes so API clients never receive HTML responses for missing endpoints, while leaving SPA/static routing behavior unchanged. Sequence diagram for JSON 404 handling of unknown API routessequenceDiagram
actor ApiClient
participant ExpressApp
participant ApiRoutes
participant Api404Middleware
participant SpaFallback
ApiClient->>ExpressApp: HTTP GET /api/nonexistent
ExpressApp->>ApiRoutes: Match registered /api routes
ApiRoutes-->>ExpressApp: No matching route
ExpressApp->>Api404Middleware: Invoke app.use('/api', ...)
Api404Middleware-->>ApiClient: 404 JSON { error: API route not found, path: /api/nonexistent }
%% Non API path still goes to SPA
ApiClient->>ExpressApp: HTTP GET /nonexistent-frontend-route
ExpressApp->>SpaFallback: Static SPA fallback
SpaFallback-->>ApiClient: 200 HTML index.html
Flow diagram for routing between API JSON 404 and SPA fallbackflowchart TD
A[Incoming HTTP request] --> B{Path starts with /api?}
B -->|Yes| C{Matches existing /api route?}
C -->|Yes| D[Handle with specific API route
status: 2xx/4xx/5xx JSON]
C -->|No| E[API 404 middleware
status: 404 JSON
error: API route not found]
B -->|No| F{Static file or SPA route?}
F -->|Static asset| G[Serve static file]
F -->|SPA route| H[Serve index.html
SPA fallback]
F -->|No match| H
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
/api/*paths by adding an explicit JSON 404 handler becauseGET /api/nonexistentpreviously fell through to the SPA/static handlers and returned HTML.Description
app.use('/api', ...)inserver.js(inserted after existing API endpoints, includingapp.get('/api/health'), and before the static SPA fallback) that returns404with JSON{"error":"API route not found","path":req.originalUrl}and only touchesserver.js, leaving auth, Stripe, usage, SEO generation, cleanse logic, Docker, metadata utilities, and frontend UI unchanged.Testing
npm installandnpm run build, started the server withNODE_ENV=development JWT_SECRET=dev_jwt_secret_change_me ENABLE_MOCK_CHECKOUT=true DB_PATH=./spectra.db FRONTEND_URL=http://localhost:5173 PORT=3001 npm start, and verified withcurlthatGET /api/healthreturned200JSON,GET /api/mewithout a token returned401JSON,GET /api/nonexistentreturned404JSON with{"error":"API route not found","path":"/api/nonexistent"}, andGET /nonexistent-frontend-routestill servedindex.html(SPA fallback) — all checks passed.Codex Task
Summary by Sourcery
Bug Fixes: