From 5074f3d9ef9b176d9a27246081999ea4140b5078 Mon Sep 17 00:00:00 2001 From: adamp Date: Tue, 10 Feb 2026 00:29:10 -0600 Subject: [PATCH] Return proper 404s for unmatched API routes and static files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a /api catch-all that returns 404 JSON for unmatched API routes. In the SPA fallback, only serve index.html for navigation requests (no file extension) — requests for non-existent static files now get a real 404 instead of index.html with 200. Co-Authored-By: Claude Opus 4.6 --- server/index.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/server/index.js b/server/index.js index 376989a..d27816b 100644 --- a/server/index.js +++ b/server/index.js @@ -44,10 +44,21 @@ app.use('/api/orders', ordersRouter); app.use('/api/dashboard', dashboardRouter); app.use('/api/reports', reportsRouter); +// 404 for unmatched API routes +app.use('/api', (req, res) => { + res.status(404).json({ error: 'Not found' }); +}); + if (process.env.NODE_ENV === 'production') { - app.use(express.static(path.join(__dirname, '..', 'client', 'dist'))); + const clientDist = path.join(__dirname, '..', 'client', 'dist'); + app.use(express.static(clientDist)); app.get('*', (req, res) => { - res.sendFile(path.join(__dirname, '..', 'client', 'dist', 'index.html')); + // Only serve index.html for navigation requests (no file extension). + // Requests for non-existent static files (e.g. /foo.js) get a real 404. + if (path.extname(req.path)) { + return res.status(404).end(); + } + res.sendFile(path.join(clientDist, 'index.html')); }); }