-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.controller.js
More file actions
105 lines (80 loc) · 2.48 KB
/
Copy pathapp.controller.js
File metadata and controls
105 lines (80 loc) · 2.48 KB
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
import dotenv from 'dotenv'
import path from "path";
dotenv.config({ path: path.resolve("./.env") });
import cookieParser from "cookie-parser";
import connectDB from "./DB/connect.js";
import { globalErrorHandler } from "./utils/response/error.response.js";
import reviewRouter from "./modules/review/review.routes.js";
import cors from "cors";
import helmet from "helmet";
import morgan from "morgan";
import { createRateLimiter } from "./utils/security/rate.limit.js";
import authRouter from "./modules/auth/auth.controller.js";
import bookingRouter from "./modules/booking/booking.controller.js";
import usersRouter from "./modules/users/users.controller.js";
import { authenticateUser } from "./middleware/authenticateUser.middleware.js";
import express from "express";
import { Server } from "socket.io";
import { initializeSocket } from './modules/chats/chat.socket.js';
import chatRouter from './modules/chats/chat.controller.js';
export const app = express();
const port = process.env.PORT || 5000;
export const initiateApp = () => {
app.set("trust proxy", 1);
app.use(helmet());
app.use(morgan("dev"));
const corsOptions = {
origin: ["http://localhost:4200", "http://127.0.0.1:5500"],
credentials: true,
allowedHeaders: ["Content-Type", "Authorization"],
methods: ["GET", "POST", "PUT", "PATCH", "DELETE"],
};
app.use(cors(corsOptions));
app.use(express.json());
app.use(cookieParser());
app.use("/api/auth", createRateLimiter(20, 15 * 60 * 1000), authRouter);
app.use(
"/api/booking",
authenticateUser(),
createRateLimiter(1000, 60 * 60 * 1000),
bookingRouter
);
app.use(
"/api/chat",
authenticateUser(),
createRateLimiter(1000, 60 * 60 * 1000),
chatRouter
);
app.use(
"/api/user",
createRateLimiter(1000, 60 * 60 * 1000),
authenticateUser(),
usersRouter
);
app.use("/api/reviews", reviewRouter);
// 404 Router
app.all("{*dummy}", (req, res) => {
res.status(404).json({
message: "Page Not Found",
info: "Place Check Your Method And URL Path",
method: req.method,
path: req.path,
});
});
app.use(globalErrorHandler);
};
export const bootstrap = async () => {
initiateApp();
await connectDB();
const httpServer = app.listen(port, () =>
console.log(`app listening on port ${port}! 🚀`)
);
const io = new Server(httpServer, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
initializeSocket(io);
};
export default bootstrap;