setup features
This commit is contained in:
+47
-5
@@ -1,17 +1,59 @@
|
||||
import express from 'express';
|
||||
import cors from 'cors';
|
||||
import dotenv from 'dotenv';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { initDB } from './db/index.js';
|
||||
import authRoutes from './routes/auth.js';
|
||||
import challengeRoutes from './routes/challenges.js';
|
||||
import predictionRoutes from './routes/predictions.js';
|
||||
import friendRoutes from './routes/friends.js';
|
||||
import tmdbRoutes from './routes/tmdb.js';
|
||||
import leaderboardRoutes from './routes/leaderboard.js';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
const app = express();
|
||||
|
||||
// Middleware
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.json({ message: "What's The Point API" });
|
||||
// API Routes
|
||||
app.use('/api/auth', authRoutes);
|
||||
app.use('/api/challenges', challengeRoutes);
|
||||
app.use('/api/predictions', predictionRoutes);
|
||||
app.use('/api/friends', friendRoutes);
|
||||
app.use('/api/tmdb', tmdbRoutes);
|
||||
app.use('/api/leaderboard', leaderboardRoutes);
|
||||
|
||||
// Health check
|
||||
app.get('/api/health', (req, res) => {
|
||||
res.json({ status: 'ok', message: "What's The Point API" });
|
||||
});
|
||||
|
||||
// Serve static frontend files (for production)
|
||||
const frontendPath = path.join(__dirname, '../../frontend/dist');
|
||||
app.use(express.static(frontendPath));
|
||||
|
||||
// Serve index.html for all non-API routes (SPA support)
|
||||
app.get('*', (req, res) => {
|
||||
res.sendFile(path.join(frontendPath, 'index.html'));
|
||||
});
|
||||
|
||||
const PORT = process.env.PORT || 4000;
|
||||
app.listen(PORT, () => {
|
||||
console.log(`API running on port ${PORT}`);
|
||||
});
|
||||
|
||||
// Initialize database and start server
|
||||
initDB()
|
||||
.then(() => {
|
||||
app.listen(PORT, '0.0.0.0', () => {
|
||||
console.log(`✅ Server running on port ${PORT}`);
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('Failed to start server:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user