From cb3dbe8d5bef90835b94f6532564f6b03d684dd2 Mon Sep 17 00:00:00 2001 From: Ryderjj89 Date: Sat, 13 Sep 2025 12:28:59 -0400 Subject: [PATCH] Add static file serving and SPA routing support for React frontend --- backend/src/index.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/backend/src/index.js b/backend/src/index.js index 13678566..942b20d0 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -12,6 +12,9 @@ app.use(helmet()); app.use(cors()); app.use(express.json()); +// Serve static files from the React build +app.use(express.static(path.join(__dirname, '../frontend/build'))); + // Bible data directory const BIBLE_DATA_DIR = path.join(__dirname, '../bible-data'); @@ -114,17 +117,17 @@ app.get('/books/:book/:chapter', async (req, res) => { } }); +// Catch-all handler: send back React's index.html for client-side routing +app.get('*', (req, res) => { + res.sendFile(path.join(__dirname, '../frontend/build/index.html')); +}); + // Error handling middleware app.use((err, req, res, next) => { console.error(err.stack); res.status(500).json({ error: 'Something went wrong!' }); }); -// 404 handler -app.use((req, res) => { - res.status(404).json({ error: 'Endpoint not found' }); -}); - // Start server app.listen(PORT, () => { console.log(`ESV Bible API server running on port ${PORT}`);