-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
90 lines (72 loc) · 2.46 KB
/
app.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
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const db = require("./config/keys").mongoURI;
const passport = require("passport");
const path = require("path");
const users = require("./routes/api/users");
const User = require("./models/User");
const dogs = require("./routes/api/dogs");
const bodyParser = require("body-parser");
const cors = require('cors')
const server = require("http").createServer(app);
const io = require("socket.io")(server, { cors: { origin: "*" } });
const {
addUser,
removeUser,
getUser,
getUsersInRoom,
} = require("./server_socketio/users_util");
if (process.env.NODE_ENV === "production") {
app.use(express.static("frontend/build"));
app.get("/", (req, res) => {
res.sendFile(path.resolve(__dirname, "frontend", "build", "index.html"));
});
}
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors())
mongoose
.connect(db, { useNewUrlParser: true })
.then(() => console.log("Connected to MongoDB successfully"))
.catch((err) => console.log(err));
app.use(passport.initialize());
require("./config/passport")(passport);
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);
const port = process.env.PORT || 5000;
// app.listen(port, () => console.log(`Server is running on port ${port}`));
server.listen(port, () => {
console.log(`Server is running on ${port}`);
});
app.use("/api/users", users);
app.use("/api/dogs", dogs);
io.on("connection", (socket) => {
socket.on("join", ({ name, room }, callback) => {
const { error, user } = addUser({ id: socket.id, name, room });
if (error) return callback("error");
socket.emit("message", {
user: "admin",
text: `${user.name}, welcome to the doghouse`,
}); // send message to user
socket.broadcast
.to(user.room)
.emit("message", { user: "admin", text: `${user.name}, has joined!` }); // send message to everyone in the room
socket.join(user.room); // add user to the room
callback();
});
socket.on("sendMessage", (message, callback) => {
const user = getUser(socket.id);
io.to(user.room).emit("message", { user: user.name, text: message }); // listens for message from user and sends message to room??
callback();
});
socket.on("disconnect", () => {
const user = removeUser(socket.id);
if(user) {
io.to(user.room).emit('message', { user: 'Admin', text: `${user.name} has left.` });
}});
});