This repository has been archived by the owner on Dec 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
app.js
78 lines (62 loc) · 2.4 KB
/
app.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
const express = require("express");
const { createServer } = require("http");
const { Server } = require("socket.io");
const { initializeSocketIO } = require("./src/socket/index.socket");
const dotenv = require("dotenv");
const cors = require("cors");
const mongoose = require("mongoose");
const dbConfig = require("./src/config/db.config");
const { response_200 } = require("./src/utils/responseCodes.utils");
const authRouter = require("./src/routes/auth.routes");
const workspaceRouter = require("./src/routes/workspace.routes");
const userRouter = require("./src/routes/user.routes");
const User = require("./src/models/user.models");
const messageRouter = require("./src/routes/message.routes");
dotenv.config();
const port = process.env.PORT || 5000;
const cookieParser = require("cookie-parser");
mongoose
.connect(`mongodb://${dbConfig.HOST}:${dbConfig.PORT}/${dbConfig.DB}`)
.then(() => {
console.log("🎉 Successfully connected to MongoDB.");
})
.catch((err) => {
console.error("💩 Failed to connect to MongoDB\n", err);
process.exit();
});
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
pingTimeout: 60000,
cors: {
origin: process.env.CORS_ORIGIN,
credentials: true,
},
});
app.set("io", io); // using set method to mount the `io` instance on the app to avoid usage of `global`
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.use(cookieParser());
// routers
app.use("/api/auth", authRouter);
app.use("/api/user", userRouter);
app.use("/api/message", messageRouter);
app.get("/", (req, res) => response_200(res, "Server is running"));
// path to get all usrs
app.get("/api/users", async (req, res) => {
const users = await User.find();
return response_200(res, "Users fetched successfully", users);
});
const workspaceRoutes = require("./src/routes/workspace.routes");
app.use("/api/workspace", workspaceRoutes);
const channelRoutes = require("./src/routes/channel.routes");
app.use("/api/channel", channelRoutes);
const subChannelRouter = require("./src/routes/subChannel.routes");
app.use("/api/subchannel", subChannelRouter);
const messageRoutes = require("./src/routes/message.routes");
app.use("/api/message", messageRoutes);
initializeSocketIO(io);
app.listen(port, () =>
console.log(`🚀 Server running at http://localhost:${port}/`)
);