-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (60 loc) · 1.81 KB
/
app.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import express from "express";
import mongoose from "mongoose";
import cors from "cors";
import dotenv from "dotenv";
import morgan from "morgan";
import cookieParser from "cookie-parser";
import path from "path";
import { fileURLToPath } from "url";
import userRoutes from "./routes/users.js";
import shoesPageRoutes from "./routes/productRoute.js";
import orderRoutes from "./routes/order.js";
// Load environment variables
dotenv.config();
// Derive __dirname from import.meta.url
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const PORT = process.env.PORT || 5000;
// Middleware
app.use(cookieParser());
app.use(express.json({ limit: "30mb" }));
app.use(express.urlencoded({ limit: "30mb", extended: true }));
app.use(
cors({
origin: process.env.ALLOWED_ORIGINS
? process.env.ALLOWED_ORIGINS.split(",")
: ["http://localhost:3000"],
credentials: true,
})
);
app.use(morgan("dev"));
// Serve static files from the "public" directory
app.use(express.static(path.join(__dirname, "public")));
// Serve the initial HTML page
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
// Routes
app.use("/user", userRoutes);
app.use("/shoesPage", shoesPageRoutes);
app.use("/order", orderRoutes);
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send("Something went wrong!");
});
// Database connection and server startup
mongoose.set("strictQuery", false);
(async () => {
try {
await mongoose.connect(process.env.CONNECTION_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
} catch (error) {
console.log(error);
}
})();
export default app;