-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (85 loc) · 3.11 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
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
// Importing Packages
const express = require("express");
const favicon = require("serve-favicon");
const mongoose = require("mongoose");
const path = require("path");
require("dotenv").config();
// Utils/Configs imports
const config = require("./config/config");
const { CreateIndexes } = require("./config/ModelIndexes");
const messages = require("./config/messages");
// Socket Setup
const http = require("http");
const socketio = require("socket.io");
const { userJoin, getCurrentUser, userLeave, getRoomUsers } = require("./utils/Socket");
// Routes imports
const BuySellItem = require("./routes/BuySellItem");
const Chats = require("./routes/Chats");
const Feedbacks = require("./routes/Feedback");
const OTP = require("./routes/OTP");
const LostFoundItems = require("./routes/LostFoundItems");
const RaisedHands = require("./routes/RaisedHands");
const Requirements = require("./routes/Requirements");
const User = require("./routes/Users");
// Connect to MongoDB
mongoose
.connect(config.db_url, config.db_config)
.then(() => {
console.log(`Connected to ${process.env.DB_Name} Mongo DB...`);
CreateIndexes();
})
.catch(error => console.error(messages.serverError, error));
// Express app initialization
const app = express();
// Configuring Express to use static files
app.use(express.static(path.join(__dirname, "/public")));
app.use(favicon(__dirname + "/favicon.ico"));
// Configuring Express app for production
require("./config/Production")(app);
// Wrapping Express app with Socket.io
const server = http.createServer(app);
const io = socketio(server);
// Socket listeners
io.on("connection", socket => {
socket.on("joinRoom", ({ username, room }) => {
const user = userJoin(socket.id, username, room);
socket.join(user.room);
io.to(user.room).emit("roomUsers", {
room: user.room,
users: getRoomUsers(user.room),
});
});
socket.on("chatMessage", msg => {
const user = getCurrentUser(socket.id);
io.to(user.room).emit("message", msg);
});
socket.on("type-update", msg => {
const user = getCurrentUser(socket.id);
io.to(user.room).emit("type-update-emitter", msg);
});
socket.on("disconnect", () => {
const user = userLeave(socket.id);
if (user) {
io.to(user.room).emit("roomUsers", {
room: user.room,
users: getRoomUsers(user.room),
});
}
});
});
// Add routes
app.use(process.env.apiVersion + process.env.buysell, BuySellItem);
app.use(process.env.apiVersion + process.env.chats, Chats);
app.use(process.env.apiVersion + process.env.feedback, Feedbacks);
app.use(process.env.apiVersion + process.env.otp, OTP);
app.use(process.env.apiVersion + process.env.lostfound, LostFoundItems);
app.use(process.env.apiVersion + process.env.raisedhands, RaisedHands);
app.use(process.env.apiVersion + process.env.requirements, Requirements);
app.use(process.env.apiVersion + process.env.auth, User);
app.use((req, res, next) => {
res.status(404).sendFile(path.join(__dirname + "/views/404.html"));
});
// Server listening on port
server.listen(config.Port, () =>
console.log(`Mode = ${process.env.NODE_ENV} and Listening on ${config.Port}..`)
);