-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
57 lines (44 loc) · 1.44 KB
/
app.js
File metadata and controls
57 lines (44 loc) · 1.44 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
require("dotenv").config();
require("express-async-errors")
const EventEmitter = require("events");
EventEmitter.defaultMaxListeners = 100;
const express = require("express")
const http = require("http");
const socketIo = require("socket.io")
const connectDB=require("./config/connect");
const notFoundMiddleware = require("./middleware/not-found")
const errorHandlerMiddleware = require("./middleware/error-handler");
const authMiddleware = require("./middleware/authentication");
const authRouter = require("./routes/auth")
const rideRouter = require("./routes/ride")
const handleSocketConnection = require("./controllers/sockets");
const app = express();
app.use(express.json());
const server = http.createServer(app)
const io = socketIo(server, {
cors: {
origin: "*"
}
})
app.use((req, res, next) => {
req.io = io;
return next();
})
handleSocketConnection(io);
app.use("/auth", authRouter);
app.use("/ride", authMiddleware, rideRouter);
app.use(notFoundMiddleware)
app.use(errorHandlerMiddleware)
const main = async () => {
try{
server.listen(process.env.PORT || 3000,"0.0.0.0",async () => {
console.log(`HTTP server is running on port http://localhost:${process.env.PORT || 3000}`)
})
await connectDB(process.env.MONGO_URI).then((data) => {
console.log(`Mongodb is connected at ${data?.connection?.host}`)
})
}catch(error){
console.log(error);
}
}
main();