-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
131 lines (114 loc) · 3.15 KB
/
Copy pathserver.js
File metadata and controls
131 lines (114 loc) · 3.15 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import express from "express";
import mongoose from "mongoose";
import cors from "cors";
import mongoData from "./mongoData.js";
import Pusher from "pusher";
import allRoutes from "./routes.js";
//app config
const app = express();
const port = process.env.PORT || 8000;
const pusher = new Pusher({
appId: "1130498",
key: "2c85310c5ba9cfaac9e1",
secret: "ef62db04fe4399937772",
cluster: "ap2",
useTLS: true,
});
//middlewares
app.use(express.json());
app.use(cors());
//db config
const mongoURI =
"mongodb+srv://admin:HVKRGjoOa3DwFsaz@cluster0.mdubn.mongodb.net/discordDB?retryWrites=true&w=majority";
mongoose.connect(mongoURI, {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoose.connection.once("open", () => {
console.log("DB Connected");
const changeStream = mongoose.connection.collection("conversations").watch();
changeStream.on("change", (change) => {
if (change.operationType === "insert") {
pusher.trigger("channels", "newChannel", {
change: change,
});
} else if (change.operationType === "update") {
pusher.trigger("conversation", "newMessage", {
change: change,
});
} else if (change.operationType === "delete") {
pusher.trigger("channels", "deleteChannel", {
change: change,
});
}
});
});
//api routes
// app.get("/", (req, res) => res.status(200).send("Hello world"));
app.use("/api", allRoutes);
// app.post("/new/channel", (req, res) => {});
// app.get("/get/channelList", (req, res) => {
// mongoData.find((err, data) => {
// if (err) {
// res.status(500).send(err);
// } else {
// let channels = [];
// data.map((channelData) => {
// const channelInfo = {
// id: channelData._id,
// name: channelData.channelName,
// };
// channels.push(channelInfo);
// });
// res.status(201).send(data);
// }
// });
// });
// app.post("/new/message", (req, res) => {
// const newMessage = req.body;
// mongoData.update(
// { _id: req.query.id },
// { $push: { conversation: req.body } },
// (err, data) => {
// if (err) {
// console.log("Error saving message...");
// console.log(err);
// res.status(500).send(err);
// } else {
// res.status(200).send(data);
// }
// }
// );
// });
// app.get("/get/data", (req, res) => {
// mongoData.find((err, data) => {
// if (err) {
// res.status(500).send(err);
// } else {
// res.status(200).send(data);
// }
// });
// });
// app.get("/get/conversation", (req, res) => {
// const id = req.query.id;
// mongoData.find({ _id: id }, (err, data) => {
// if (err) {
// res.status(500).send(err);
// } else {
// res.status(200).send(data);
// }
// });
// });
// app.delete("/delete/channel/:id", (req, res) => {
// const channel = req.params.id;
// mongoData.deleteOne({ _id: channel }, (err, data) => {
// if (err) {
// res.status(500).send(err);
// } else {
// res.status(201).send(data);
// }
// });
// });
//listen
app.listen(port, () => console.log(`listening on localhost:${port}`));