-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (79 loc) · 2.41 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const path = require("path");
const express = require("express");
const dotenv = require("dotenv");
dotenv.config({ path: "./src/config/config.env" });
const morgan = require("morgan");
const errorHandler = require("./src/middlewares/error");
const Emitter=require('events');
const fileUpload = require("express-fileupload");
const cookieParser = require("cookie-parser");
const connectDB = require("./src/config/db");
const cors = require("cors");
const http = require("http");
const {
authRoute,
placeRoute,
categoryRoute,
extraRoute,
productRoute,
reviewRoute,
favoriteRoute,
orderRoute,
} = require("./src/routes/index.routes");
connectDB();
const app = express();
const server = http.createServer(app);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors({ origin: "*" }));
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
const eventEmitter=new Emitter()
app.set('eventEmitter',eventEmitter)
const PORT = process.env.PORT || 8000;
const io = require("socket.io")(server, {
cors: {
origin: "*",
},
});
io.on("connection", (socket) => {
console.log(socket.id);
socket.on("join", (orderId) => {
socket.join(orderId);
socket.join("orders");
});
socket.on("newOrder", (order) => {
console.log("novos pedidos",order)
socket.join(order.data._id);
socket.emit("orders",order)
});
});
eventEmitter.on("orderUpdated", (data) => {
io.to(`order_${data.id}`).emit("orderUpdated", data);
console.log(data)
io.to(`adminRoom`).emit("orderUpdated", data);
});
eventEmitter.on("orderPlaced", (data) => {
io.to(`adminRoom`).emit("orderPlaced", data);
});
app.use(fileUpload());
app.use(express.static(path.join(__dirname, "public")));
//routes
app.use("/api/v1/auth", authRoute);
app.use("/api/v1/places", placeRoute);
app.use("/api/v1/categories", categoryRoute);
app.use("/api/v1/extra", extraRoute);
app.use("/api/v1/products", productRoute);
app.use("/api/v1/reviews", reviewRoute);
app.use("/api/v1/favorites", favoriteRoute);
app.use("/api/v1/orders", orderRoute);
//handle errors
app.use(errorHandler);
server.listen(PORT, () => {
console.log("listening on ",PORT);
});
server.on("unhandledRejection", (err, promise) => {
console.log(`Erro: ${err.message}`);
server.close(() => process.exit(1));
});