-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
127 lines (107 loc) · 2.95 KB
/
server.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const path = require("path");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 5000;
const server = require("http").createServer(app);
const io = require("socket.io")(server);
app.use(cors());
app.use(express.json());
const dir = path.join(__dirname, "uploads");
app.use("/uploads", express.static(dir));
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});
const connection = mongoose.connection;
connection.once("open", () => {
console.log("MongoDB database connection established successfully");
});
const listingsRouter = require("./routes/listings");
const usersRouter = require("./routes/users");
const chatsRouter = require("./routes/chats");
app.use("/api/listings", listingsRouter);
app.use("/api/users", usersRouter);
app.use("/api/chats", chatsRouter);
// Socket.io
const Chat = require("./models/chat.model");
io.on("connection", (socket) => {
console.log("user connected");
socket.on(
"Chat Sent",
({ chatMessage, senderId, receiverId, listingId, date }, callback) => {
const chat = new Chat({
message: chatMessage,
writer: senderId,
receiver: receiverId,
listing: listingId,
date,
});
chat
.save()
.then((res) => {
Chat.findById(res._id)
.populate("writer")
.populate("receiver")
.populate("listing")
.exec((chatErr, chatDoc) => {
if (chatErr) return callback(chatErr);
return io.emit("Output Chat Sent", chatDoc);
});
})
.catch((err) => {
callback(err);
});
}
);
socket.on(
"Offer Sent",
(
{ chatMessage, offerPrice, senderId, receiverId, listingId, date },
callback
) => {
const chat = new Chat({
message: chatMessage,
writer: senderId,
receiver: receiverId,
offerPrice,
listing: listingId,
date,
});
chat
.save()
.then((res) => {
Chat.findById(res._id)
.populate("writer")
.populate("receiver")
.populate("listing")
.exec((chatErr, chatDoc) => {
if (chatErr) return callback(chatErr);
return io.emit("Output Offer Sent", chatDoc);
});
})
.catch((err) => {
callback(err);
});
}
);
socket.on("disconnect", () => {
console.log("user disconnected");
});
});
if (
process.env.NODE_ENV === "production" ||
process.env.NODE_ENV === "staging"
) {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.join(`${__dirname}/client/build/index.html`));
});
}
server.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});