-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
107 lines (86 loc) · 2.75 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import "dotenv/config";
import express from "express";
import "express-async-errors";
import mongoose from "mongoose";
import helmet from "helmet";
import cors from "cors";
import rateLimit from "express-rate-limit";
import session from "express-session";
import passport from "passport";
import "./utils/passport.js";
//error handler
import errorHandlerMiddleware from "./middleware/error-handler.js";
import notFoundMiddleware from "./middleware/not-found.js";
//import route
import userRouter from "./routes/user.js";
import houseRouter from "./routes/house.js";
import carRouter from "./routes/car.js";
import yatchRouter from "./routes/yatch.js";
import bookingRouter from "./routes/booking.js";
import paymentRouter from "./routes/payment.js";
import notificationRouter from "./routes/notification.js";
import ratingRouter from "./routes/rating.js";
const app = express();
const port = process.env.PORT || 8000;
app.set("trust proxy", 1);
app.use(
rateLimit({
windowMs: 15 * 60 * 1000, //15 mins
max: 500, //limit each ip to 100 requests per windowsMs
})
);
app.use(express.json());
app.use(helmet());
app.use(express.urlencoded({ extended: true }));
// Define a whitelist of allowed origins
const whitelist = ["http://localhost:3000", "https://travle-leaf.onrender.com", ];
// Define the CORS options
const corsOptions = {
origin: (origin, callback) => {
if (!origin || whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
optionsSuccessStatus: 200, // Some legacy browsers (IE11, various SmartTVs) choke on 204
methods: ["GET", "POST", "DELETE", "PUT", "PATCH"],
credentials: true,
};
// Enable CORS with the specified options
app.use(cors(corsOptions));
app.get("/", (req, res) => {
res.send(
`Travel Leaf API <p>Checkout the <a href="https://documenter.getpostman.com/view/31014226/2sA35K1LDs">Travel Leaf API Documentation</a></p>`
);
});
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use("/auth/google", cors(corsOptions));
app.use("/auth/google/callback", cors(corsOptions));
app.use("/", userRouter);
app.use("/house", houseRouter);
app.use("/car", carRouter);
app.use("/yatch", yatchRouter);
app.use("/booking", bookingRouter);
app.use("/payment", paymentRouter);
app.use("/rating", ratingRouter);
app.use("/notification", notificationRouter);
app.use(notFoundMiddleware);
app.use(errorHandlerMiddleware);
const start = async () => {
try {
await mongoose.connect(process.env.MONGO_URI);
app.listen(port, () => console.log(`server is running on port ${port}`));
} catch (error) {
console.log(error);
}
};
start();