-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathapp.js
More file actions
60 lines (47 loc) Β· 1.47 KB
/
Copy pathapp.js
File metadata and controls
60 lines (47 loc) Β· 1.47 KB
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
import dotenv from 'dotenv';
import 'express-async-errors';
import EventEmitter from 'events';
import express from 'express';
import http from 'http';
import { Server as socketIo } from 'socket.io';
import connectDB from './config/connect.js';
import notFoundMiddleware from './middleware/not-found.js';
import errorHandlerMiddleware from './middleware/error-handler.js';
import authMiddleware from './middleware/authentication.js';
// Routers
import authRouter from './routes/auth.js';
import rideRouter from './routes/ride.js';
// Import socket handler
import handleSocketConnection from './controllers/sockets.js';
dotenv.config();
EventEmitter.defaultMaxListeners = 20;
const app = express();
app.use(express.json());
const server = http.createServer(app);
const io = new socketIo(server, { cors: { origin: "*" } });
// Attach the WebSocket instance to the request object
app.use((req, res, next) => {
req.io = io;
return next();
});
// Initialize the WebSocket handling logic
handleSocketConnection(io);
// Routes
app.use("/auth", authRouter);
app.use("/ride", authMiddleware, rideRouter);
// Middleware
app.use(notFoundMiddleware);
app.use(errorHandlerMiddleware);
const start = async () => {
try {
await connectDB(process.env.MONGO_URI);
server.listen(process.env.PORT || 3000, "0.0.0.0", () =>
console.log(
`HTTP server is running on port http://localhost:${process.env.PORT || 3000}`
)
);
} catch (error) {
console.log(error);
}
};
start();