-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
83 lines (70 loc) · 2.23 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import express from "express";
import "express-async-errors";
import dotenv from "dotenv";
import cookieParser from "cookie-parser";
import helmet from "helmet";
import xss from "xss-clean";
import ExpressMongoSanitize from "express-mongo-sanitize";
import path from "path";
import { fileURLToPath } from "url";
dotenv.config();
const app = express();
// connectDB
import connectDB from "./db/connectDb.js";
// import routes
import authRouter from "./routes/AuthRoute.js";
import categoryRouter from "./routes/CategoryRoute.js";
import productsRouter from "./routes/ProductsRoute.js";
import reviewRouter from "./routes/ReviewRoute.js";
import orderRouter from "./routes/OrderRoute.js";
import clientRouter from "./routes/ClientRoute.js";
// import midlewares
import notFoundMiddleware from "./middleware/notFoundMiddleware.js";
import errorHandlerMiddleware from "./middleware/errorHandler.js";
app.get("/api/v1", (req, res) => {
res.json({ msg: "welcome to my mern project" });
});
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// only when ready to deploy
app.use(express.static(path.resolve(__dirname, "./client/build")));
app.use(express.json({ limit: "20mb" }));
app.use(cookieParser(process.env.JWT_SECRET));
app.use(
helmet.contentSecurityPolicy({
directives: {
scriptSrc: [
"'self'",
"data:",
"js.stripe.com",
"https://accounts.google.com",
],
imgSrc: ["'self'", "data:", "res.cloudinary.com"],
frameSrc: ["'self'", "https://js.stripe.com"],
connectSrc: ["'self'", "data:", "https://www.googleapis.com/"],
},
})
);
app.use(xss());
app.use(ExpressMongoSanitize());
// routes
app.use("/api/v1/auth", authRouter);
app.use("/api/v1/categories", categoryRouter);
app.use("/api/v1/products", productsRouter);
app.use("/api/v1/reviews", reviewRouter);
app.use("/api/v1/orders", orderRouter);
app.use("/", clientRouter);
// middlewares
app.use(notFoundMiddleware);
app.use(errorHandlerMiddleware);
const port = process.env.port || 5000;
const start = async () => {
try {
await connectDB(process.env.MONGO_URL);
app.listen(port, () => {
console.log("the app is listening on port : " + port);
});
} catch (error) {
console.log(error);
}
};
start();