-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathserver.js
110 lines (89 loc) · 2.66 KB
/
server.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
108
109
110
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const path = require("path");
const app = express();
const server = require("http").Server(app);
const io = require("socket.io")(server);
const {
createRoom,
getRoomByUser,
getRoom,
chatRoomMW,
roomMW,
} = require("./controllers/Room/index");
const { sendInvite, mailFeedback } = require("./controllers/Misc");
const { createUser, addRooms, getUser } = require("./controllers/User");
const { createMessage, getMessages } = require("./controllers/Message");
app.use(express.json());
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
// <---------- VIEWS ---------->
app.get("/", (req, res) => {
res.redirect(`/home`);
});
app.get("/timeline", (req, res) => {
res.render("timeline");
});
app.get("/landing", (req, res) => {
res.render("landing");
});
app.get("/home", (req, res) => {
res.render("home");
});
app.get("/aibot", (req, res) => {
res.render("aibot");
});
// <---------- ROUTES ---------->
app.post("/invite", sendInvite);
app.post("/feedback", mailFeedback);
app.post("/room", createRoom);
app.post("/room/get", getRoom);
app.post("/room/user", getRoomByUser);
app.post("/room/add", addRooms);
app.post("/message/get", getMessages);
app.post("/createuser", createUser);
app.post("/user/get", getUser);
// <---------- MW-VIEWS ---------->
app.get("/chat/:room", chatRoomMW);
app.get("/:room", roomMW);
// <---------- SOCKET ---------->
io.on("connection", (socket) => {
socket.on("join-room", (roomId, userId) => {
console.log(roomId, userId);
socket.join(roomId);
socket.to(roomId).broadcast.emit("user-connected", userId);
// messages
socket.on("message", async ({ message, user }) => {
//send message to the same room
console.log(user);
let messageResp = await createMessage(user, message, roomId);
io.to(roomId).emit("createMessage", { message: messageResp, user });
});
socket.on("disconnect", () => {
socket.to(roomId).broadcast.emit("user-disconnected", userId);
});
});
});
// <--------- MONGOOSE + SERVER START --------->
mongoose
.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
poolSize: 10, //increase poolSize from default 5
})
.then(() => {
console.log("Connected MONGODB");
server.listen(process.env.PORT || 3030, () => {
console.log(
`listening on port: ${process.env.PORT || 3030}, http://localhost:${
process.env.PORT || 3030
}`
);
});
})
.catch((err) => {
console.error(err);
});