forked from SahilKishor21/WorkQueue-back
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
55 lines (41 loc) · 1.36 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
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const connectDB = require('./config/db');
const userRoutes = require('./routes/userRoutes');
const adminRoutes = require('./routes/adminRoutes');
const headRoutes = require('./routes/headRoutes');
require('dotenv').config();
const app = express();
// Create an HTTP server instance to bind with Socket.IO
const server = http.createServer(app);
app.use('/uploads', express.static('uploads'));
const io = new Server(server, {
cors: {
origin: '*',
methods: ['GET', 'POST'],
},
});
// Database
connectDB();
// Middleware
app.use(express.json());
// Socket.IO Connection Logic
io.on('connection', (socket) => {
console.log(`New client connected: ${socket.id}`);
// Handle custom events or messages if needed
socket.on('message', (data) => {
console.log(`Message received: ${data}`);
});
socket.on('disconnect', () => {
console.log(`Client disconnected: ${socket.id}`);
});
});
// Routes
app.use('/api/users', userRoutes);
app.use('/api/admins', adminRoutes);
app.use('/api/head', headRoutes);
app.set('io', io); // with this we can use app.get('io') to access it in other files
// Start Server
const PORT = process.env.PORT || 5000;
server.listen(PORT, () => console.log(`Server started on port ${PORT}`));