-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
75 lines (62 loc) · 1.88 KB
/
Copy pathserver.js
File metadata and controls
75 lines (62 loc) · 1.88 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
import dotenv from "dotenv";
dotenv.config();
import express from "express";
import cors from "cors";
import cookieParser from "cookie-parser";
import http from "http";
import { Server } from "socket.io";
import { connectDB } from "./utils/db.js";
import authRoutes from "./routes/authRoutes.js";
import conversationRoutes from "./routes/conversationRoutes.js";
import messageRoutes from "./routes/messageRoutes.js";
import { initializeSocket } from "./socket.js";
import { socketAuthMiddleware } from "./socket/socketAuthMiddleware.js";
import RedisService from "./services/RedisService.js";
import upladRoutes from "./routes/uploadRoutes.js";
import utilRoutes from "./routes/utilRoutes.js";
const app = express();
const httpServer = http.createServer(app);
app.use(
cors({
origin: [
process.env.CLIENT_ORIGIN,
"https://frontend-nu-eight-40.vercel.app",
"https://frontend-git-main-biswajit-shaws-projects.vercel.app",
],
credentials: true,
}),
);
app.use(cookieParser());
app.use(express.json());
//routes
app.use("/api/auth", authRoutes);
app.use("/api/conversations", conversationRoutes);
app.use("/api/conversations", messageRoutes);
app.use("/api/upload", upladRoutes);
app.use("/api/utils", utilRoutes);
const io = new Server(httpServer, {
cors: {
origin: [
process.env.CLIENT_ORIGIN,
"https://frontend-nu-eight-40.vercel.app",
"https://frontend-git-main-biswajit-shaws-projects.vercel.app",
],
credentials: true,
methods: ["GET", "POST"],
},
pingInterval: 25000,
pingTimeout: 60000,
});
io.use(socketAuthMiddleware);
await initializeSocket(io);
await RedisService.initialize();
try {
await connectDB();
const PORT = process.env.PORT;
httpServer.listen(PORT, () => {
console.log(`Server running on port: ${PORT}`);
});
} catch (error) {
console.error("The server failed to start", error);
process.exit(1);
}