-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (61 loc) · 1.85 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
import { createServer } from "http";
import { Server } from "socket.io";
import connectDB from "./config/db.js";
import userRoutes from "./routes/user.routes.js";
import adminRoutes from "./routes/admin.routes.js";
import channelRoutes from "./routes/channel.routes.js";
import moderatorRoutes from "./routes/moderator.routes.js";
import groupRoutes from "./routes/group.routes.js";
import express from "express";
import cookieParser from "cookie-parser";
import morgan from "morgan";
import cors from "cors";
import { notFound, errorHandler } from "./middleware/error.middleware.js";
import dotenv from "dotenv";
import { listenForChannel } from "./sockets/index.js";
dotenv.config();
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
path: "/socket.io",
});
const port = process.env.PORT || 8000;
const corsOptions = {
origin: [
"http://192.168.100.214:5173",
"http://localhost:5173",
"https://student-online-community-frontend.vercel.app",
"https://guided-frontend.vercel.app/"
],
credentials: true,
};
app.use(cors(corsOptions));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(morgan("dev"));
connectDB();
io.on("connection", (socket) => {
console.log("connected!! " + socket.id);
listenForChannel(socket, io);
socket.on("disconnect", () => {
console.log("Disconnected " + socket.id);
});
});
app.get("/", (req, res) => {
res.send("API is running...");
});
app.use("/api/users", userRoutes);
app.use("/api/admin", adminRoutes);
app.use("/api/channels", channelRoutes);
app.use("/api/moderator", moderatorRoutes);
app.use("/api/group", groupRoutes);
app.use(notFound);
app.use(errorHandler);
httpServer.listen(port, () => {
console.log(`Server running on port ${port}`);
});