-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
133 lines (106 loc) · 3.87 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const {MONGOURI} = require("./config/keys");
const mongoose = require("mongoose");
const {io, express, app, server} = require('./utils');
const textChannelNamespace = require('./namespaces/text_channel.namespace');
const PORT = process.env.PORT || 8000;
//Middlewares
app.use(express.json());
app.set('views', './frontend/views');
app.set('view engine', 'ejs');
app.use('/frontend', express.static('./frontend/'));
app.use(require('./routes/profile.route'));
app.use(require('./routes/auth.route'));
app.use(require('./routes/group.route'));
//Connect to the Mongodb
mongoose.connect(MONGOURI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
//Check if the connection has been established
mongoose.connection.on("connected", () => {
console.log("Connected to Mongodb atlas!");
});
//But if an error is there
mongoose.connection.on("error", (err) => {
console.log("Error connecting mongodb : " + err);
});
//contains the users information
// const users = {};
// //listen for connections
// io.on("connection" , (socket) => {
// });
textChannelNamespace();
require('./namespaces/profile.namespace')();
// const textChannels = io.of(/^\/(\w+)\/textchannel\/(\w+)$/);
// const users = {};
// textChannels.on('connection', (socket) => {
// const channel = socket.nsp;
// console.log(socket);
// console.log(channel);
// let userId = null;
// //When an User joins the channel
// socket.on('join', (userDetails) => {
// //Check if the user is already connected or not
// if(!userDetails.alreadyConnected){
// users[userDetails._id] = {
// socketId: socket.id,
// name: userDetails.name,
// currentTextChannel: userDetails.currentChannelId,
// currentGroup: userDetails.currentGroup
// }
// userId = userDetails._id;
// channel.to(socket.id).emit("token", {connected: userDetails.currentChannelId});
// }else{
// //If the user is already connected
// //then disconnect the user
// socket.disconnect();
// }
// });
// socket.on("sendMessage" , (message) => {
// socket.broadcast.emit("receiveMessage" ,
// {name : users[userId].name, message});
// });
// //on sending a file
// socket.on("fileSent", (file, filetype, fileName) => {
// socket.broadcast.emit("receivedFile", {file, filetype, fileName, name: users[userId].name});
// });
// //On sending an image
// socket.on("sendImage", (data) => {
// console.log("The image data : " + data.data);
// socket.broadcast.emit("receivedImage", {name: users[userId].name, data: data.data });
// });
// //On sending a video
// socket.on("sentVideo", (data) => {
// console.log("The video: " + data);
// socket.broadcast.emit("receivedVideo", {name: users[userId].name, data: data});
// });
// //on sending a voice message
// socket.on("sentVoiceMessage" , (chunks) => {
// console.log("Voice message chunks: " + chunks);
// socket.broadcast.emit("receivedVoice" , chunks , users[userId].name);
// });
// socket.on("disconnect" , () => {
// socket.broadcast.emit("userLeft" , users[userId].name);
// delete users[userId];
// console.log(userId + " left");
// // console.log(users);
// });
// });
app.get('/', (req , res) => {
// res.render('chat', {data});
res.render('home');
});
app.use((req, res, next) => {
const error = new Error('Not Found!');
error.status = 404;
next(error);
});
app.use((err, req, res, next) => {
return res.status(err.status).render('error', {
status: err.status,
message: err.message
});
});
server.listen(PORT , () => {
console.log("Express server is listening on port number 8000...");
});