-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
70 lines (58 loc) · 1.72 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
import express from "express";
import mongoose from "mongoose";
import cors from "cors";
import dotenv from "dotenv";
import {
errorHandlerMiddleware,
notFoundHandlerMiddleware,
} from "./middleware/allErrorMiddleware.js";
import authRoute from "./routes/authRoute.js";
import productRoute from "./routes/productRoute.js";
import userRoute from "./routes/userRoute.js";
import orderRoute from "./routes/orderRoute.js";
import reviewRoute from "./routes/reviewRoute.js";
import helmet from "helmet";
import xss from "xss-clean";
import rateLimiter from "express-rate-limit";
import fileUpload from "express-fileupload";
import mongoSanitize from "express-mongo-sanitize";
dotenv.config();
const app = express();
app.set("trust-proxy", 1);
app.use(
rateLimiter({
windowMs: 15 * 60 * 1000, //15 minutes
max: 200, //limit each ip to 200 request per windowMs
})
);
app.use(express.json());
app.use(cors());
app.use(helmet());
app.use(xss());
app.use(mongoSanitize());
app.use(express.static("./public"));
app.use(fileUpload());
app.get("/", (req, res) => {
res.status(200).send({ msg: "This is a E-commerce Api" });
});
//route define here
app.use("/auth", authRoute);
app.use("/users", userRoute);
app.use("/products", productRoute);
app.use("/orders", orderRoute);
app.use("/reviews", reviewRoute);
app.use(errorHandlerMiddleware);
app.use(notFoundHandlerMiddleware);
const PORT = process.env.PORT || 5000;
const DATABASE_URL = process.env.MONGO_URI;
mongoose
.connect(DATABASE_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() =>
app.listen(PORT, () =>
console.log(`Server Running on Port: http://localhost:${PORT}`)
)
)
.catch((error) => console.log(`${error} did not connect`));