From c11a7d47b715ce4f0a1b396c83ff4fd107e03459 Mon Sep 17 00:00:00 2001 From: Sebastian Slettebakken <43045439+sebastas@users.noreply.github.com> Date: Thu, 13 Nov 2025 00:45:38 +0100 Subject: [PATCH] feat: serve built frontend and add SPA fallback for non-API routes --- backend/server.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/backend/server.js b/backend/server.js index 06bc445..5ca2c41 100644 --- a/backend/server.js +++ b/backend/server.js @@ -155,6 +155,19 @@ app.delete('/api/counters/:id', async (req, res) => { } }) + +// Serve built frontend (production) if it exists +const FRONTEND_DIST = path.join(__dirname, '..', 'frontend', 'dist') +if (fs.existsSync(FRONTEND_DIST)) { + app.use(express.static(FRONTEND_DIST)) + + // SPA fallback: serve index.html for any non-API route + app.get('/*path', (req, res) => { + if (req.path.startsWith('/api')) return res.status(404).end() + res.sendFile(path.join(FRONTEND_DIST, 'index.html')) + }) +} + // graceful shutdown: ensure Prisma disconnects process.on('SIGINT', async () => { log.info('shutting down')