-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
55 lines (46 loc) · 1.48 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
48
49
50
51
52
53
54
55
import express from "express";
import dotenv from "dotenv";
import morgan from "morgan";
import connectDb from "./config/db.js";
import authRoutes from "./routes/authRoute.js";
import cors from "cors";
import categoryRoutes from "./routes/categoryRoute.js";
import productRoutes from "./routes/ProductRoutes.js";
import path from "path";
import { fileURLToPath } from "url";
//configure env like this. if server.js and .env together in RootDirectory.
dotenv.config();
//database config
connectDb();
//ES module fix for production
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
console.log(__dirname)
//rest object
const app = express();
//middleware
app.use(cors());
app.use(express.json());
app.use(morgan("dev"));
// Serve static files from the React app
app.use(express.static(path.join(__dirname, "./client/build")));
//routes
app.use("/api/v1/auth", authRoutes);
app.use("/api/v1/category", categoryRoutes);
app.use("/api/v1/product", productRoutes);
// // rest API for local computer
// app.get('/', (req, res) => {
// res.send("<h1>Welcome to ecommerce MERN </h1>");
// });
// Rest API - Handle React routing, return all requests to React app
//Render client for any path
app.get('*', function (req, res) {
const index = path.join(__dirname, './client/build', 'index.html');
res.sendFile(index);
});
//port
const PORT = process.env.PORT || 8080;
// App listen
app.listen(PORT, () => {
console.log(`Server is Running on ${PORT}`);
});