-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
94 lines (69 loc) · 2.13 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
import express from 'express';
import { Server } from 'socket.io';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import dotenv from 'dotenv';
import authRoutes from './routes/auth.routes.js';
import userRoutes from './routes/user.routes.js';
import dbConnect from './config/dbConnect.js';
import messageRoutes from './routes/message.routes.js';
import path from 'path';
import http from 'http';
dotenv.config();
const PORT = process.env.PORT || 5000;
const app = express();
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: `http://localhost:${PORT}`,
methods: ["GET", "POST"],
},
});
// Middleware
app.use(express.json());
app.use(cookieParser());
app.use(cors());
// Connect to MongoDB
dbConnect();
const __dirname = path.resolve();
app.use(express.static(path.join(__dirname, "client")));
// Socket.io user socket map
const userSocketMap = {}; // {userId: socketId}
io.on("connection", (socket) => {
console.log("a user connected", socket.id);
const userId = socket.handshake.query.userId;
if (userId != "undefined") userSocketMap[userId] = socket.id;
// Emit the list of online users
io.emit("getOnlineUsers", Object.keys(userSocketMap));
// Handle disconnection
socket.on("disconnect", () => {
console.log("user disconnected", socket.id);
delete userSocketMap[userId];
io.emit("getOnlineUsers", Object.keys(userSocketMap));
});
});
export const getReceiverSocketId = (receiverId) => {
return userSocketMap[receiverId];
};
// API routes
app.use('/api/user', userRoutes);
app.use('/api/auth', authRoutes);
app.use("/api/messages", messageRoutes);
// Serve the frontend
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'));
});
// Global error handler
app.use((err, req, res, next) => {
const statusCode = err.statusCode || 500;
const message = err.message || 'Internal Server Error';
res.status(statusCode).json({
success: false,
statusCode,
message,
});
});
server.listen(PORT, () => {
console.log(`Server is running on port http://127.0.0.1:${PORT}/`);
});
export { app, io, server };