-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
45 lines (34 loc) · 1.13 KB
/
index.js
File metadata and controls
45 lines (34 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import productRoutes from './routes/product.route.js';
import express from 'express';
import { connectDB } from './config/db.js';
import path from 'path';
import { fileURLToPath } from 'url';
import cors from 'cors';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
// Enable CORS for all routes
app.use(cors());
// Parse JSON bodies
app.use(express.json());
// API routes go here
app.use("/api/products", productRoutes);
if(process.env.NODE_ENV === 'production'){
// Serve static files from the frontend build directory
app.use(express.static(path.join(__dirname, 'frontend/dist')));
// Handle all other routes by serving the index.html
app.get('*', (req, res) => {
console.log('Serving index.html'); // Debug log
res.sendFile(path.join(__dirname, 'frontend/dist/index.html'));
});
}
const PORT = process.env.PORT || 3000;
// Update the listen callback to include error handling
app.listen(PORT, '0.0.0.0', (err) => {
if (err) {
console.error('Error starting server:', err);
return;
}
connectDB();
console.log(`Server is running on port: ${PORT}`);
});