-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
47 lines (38 loc) · 1.28 KB
/
server.js
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
46
47
import express from "express";
import colors from "colors";
import cors from "cors";
import dotenv from 'dotenv';
import morgan from 'morgan';
import connectDb from "./config/db.js";
import authRoute from "./routes/authRoute.js";
import CategoryRoutes from "./routes/CategoryRoutes.js";
import ProductRoutes from "./routes/ProductRoutes.js";
import path from 'path';
import { fileURLToPath } from 'url';
// Configure environment variables
dotenv.config();
// Database connection
connectDb();
const app = express();
// Middleware
app.use(cors());
app.use(express.json());
app.use(morgan('dev'));
// Get the directory name of the current module (in ES module scope)
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Serve static files from the React app (build folder)
app.use(express.static(path.join(__dirname, './client/build')));
// API routes
app.use('/api/v1/auth', authRoute);
app.use('/api/v1/category', CategoryRoutes);
app.use('/api/v1/product', ProductRoutes);
// Catch-all route to serve React app
app.use('*', (req, res) => {
res.sendFile(path.join(__dirname, './client/build/index.html'));
});
// Start the server
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server running on PORT: ${PORT}`.bgGreen);
});