Add static file serving and SPA routing support for React frontend

This commit is contained in:
Ryderjj89
2025-09-13 12:28:59 -04:00
parent ce89f120f1
commit cb3dbe8d5b

View File

@@ -12,6 +12,9 @@ app.use(helmet());
app.use(cors()); app.use(cors());
app.use(express.json()); app.use(express.json());
// Serve static files from the React build
app.use(express.static(path.join(__dirname, '../frontend/build')));
// Bible data directory // Bible data directory
const BIBLE_DATA_DIR = path.join(__dirname, '../bible-data'); 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 // Error handling middleware
app.use((err, req, res, next) => { app.use((err, req, res, next) => {
console.error(err.stack); console.error(err.stack);
res.status(500).json({ error: 'Something went wrong!' }); res.status(500).json({ error: 'Something went wrong!' });
}); });
// 404 handler
app.use((req, res) => {
res.status(404).json({ error: 'Endpoint not found' });
});
// Start server // Start server
app.listen(PORT, () => { app.listen(PORT, () => {
console.log(`ESV Bible API server running on port ${PORT}`); console.log(`ESV Bible API server running on port ${PORT}`);