-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (116 loc) · 3.54 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
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
134
135
136
137
138
139
140
141
142
143
144
145
import dotenv from 'dotenv/config'
import express from "express";
import cors from "cors";
import multer from "multer";
import Users from './Schema/Users.js';
// import dirname from "path";
import mongoose from 'mongoose';
import bodyParser from "body-parser";
import Router from "./Routers/Authentication.js";
import session from 'express-session';
import passport from 'passport';
import { Strategy as LocalStrategy } from 'passport-local';
// import cons from "consolidate";
import { fileURLToPath} from "url";
import path from "path";
import chatRouter from './Routers/ChatRouter.js';
import http from 'http'
import { Server as SocketIOServer } from 'socket.io';
import createMemoryStore from "memorystore";
import { Db_name } from './constants.js';
import MongoStore from 'connect-mongo';
// dotenv.config()
// console.log(process.env)
var upload = multer();
const app = express();
const server = http.createServer(app); // Create an HTTP server
app.use(express.json());
app.use(cors({
origin: "*"
}
));
// connecting to mongodb
async function connectToDB(){
try {
console.log(`${process.env.MONGO_URI}/${Db_name}`)
await mongoose.connect(`${process.env.MONGO_URI}/${Db_name}`)
console.log("connected to databases")
} catch (error) {
console.log("an error has been occured while connecting database", error)
throw new Error("an error has been occured ")
}
}
connectToDB();
// Middleware
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
store: MongoStore.create({
mongoUrl: `${process.env.MONGO_URI}/${Db_name}`,
collectionName: 'sessions'
}),
cookie: {
maxAge: 60000, // Adjust as necessary
secure: process.env.NODE_ENV === 'production', // Only send over HTTPS in production
},
}));
app.use(passport.initialize());
app.use(passport.session());
var User = mongoose.model('Users');
passport.use(new LocalStrategy(Users.authenticate()));
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser(async function (id, done) {
try {
const user = await User.findById(id);
done(null, user);
} catch (err) {
console.error('Error in deserializeUser:', err);
done(err, null);
}
});
app.get("/",(req,res)=>{
res.send("hello there")
})
console.log('Initializing /users routes');
app.use("/users", Router);
console.log('Initializing /chats routes');
app.use("/chats", chatRouter);
// Start the server
const port = process.env.PORT || 5000;
server.listen(port, () => {
console.log(`Server is running on port :${port}`);
});
const io = new SocketIOServer(server, {
pingTimeout: 6000,
cors: {
origin: "*"
},
});
io.on("connect", (socket) => {
socket.on("setup", (userData) => {
socket.join(userData.user._id);
socket.emit("connected");
})
socket.on("join chat", (room) => {
socket.join(room);
})
socket.on("new message", (newMessageRecieved) => {
var chat = newMessageRecieved.chat;
if (!chat.users) return console.log("chat.users not defined");
chat.users.forEach((user) => {
if (user._id == newMessageRecieved.sender._id) return;
socket.in(user._id).emit("message recieved", newMessageRecieved);
});
});
socket.on("connect_error", (err) => {
console.log("error", err);
});
// Handle socket events here
socket.off("setup", (userData) => {
console.log("user is disconnected")
socket.leave(userData.user._id);
});
});