-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
54 lines (45 loc) · 1.79 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
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const cors = require('cors');
const dotenv = require('dotenv');
const { connectDB } = require('./config/db');
const authRoutes = require('./routes/auth');
const contactsRoutes = require('./routes/userContacts');
const messageRoutes = require('./routes/messages');
const accountRoutes = require('./routes/userAccount');
const groupRoutes = require('./routes/userGroups');
const { handleSocket } = require('./controllers/messagesController');
// Load environment variables
dotenv.config();
// Initialize Express app and HTTP server
const app = express();
const server = http.createServer(app);
// Initialize Socket.io
const io = new Server(server, {
cors: {
origin: '*', // Allow all origins for testing
methods: ['GET', 'POST'],
},
});
const corsOptions = {
origin: '*', // Allow all origins for testing
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
credentials: true
};
// Middleware
app.use(cors(corsOptions)); // Enable CORS with the specified options
app.use(express.json()); // Parse incoming JSON requests
// Connect to MongoDB
connectDB();
// API Routes
app.use('/api/auth', authRoutes); // Authentication routes
app.use('/api/contacts', contactsRoutes); // User contacts routes
app.use('/api/messages', messageRoutes); // Messaging routes
app.use('/api/account', accountRoutes); // User account routes
app.use('/api/groups', groupRoutes); // User groups routes
// Initialize Socket.io for handling messages
handleSocket(io);
// Start the server
const PORT = process.env.PORT || 5000;
server.listen(PORT, '0.0.0.0', () => console.log(`Server running on http://0.0.0.0:${PORT}`)); // Listen on the specified port (if running locally) or 5000 (if running on Heroku)