-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
74 lines (61 loc) · 1.92 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
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const server = require(`http`).Server(app);
const io = require("socket.io")(server);
const compression = require("compression");
const doctorRoutes = require("./routes/doctorRoutes");
const patientRoutes = require("./routes/patientRoutes");
const requestRoutes = require("./routes/requestRoutes");
//CORS
app.use((req, res, next) => {
res.append("Access-Control-Allow-Origin", ["*"]);
res.append("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
res.append("Access-Control-Allow-Headers", "Content-Type, Authorization");
next();
});
// COMPRESSION --> GZIP
app.use(compression());
// Body-parser
app.use(bodyParser.json());
app.use(doctorRoutes);
app.use(patientRoutes);
app.use(requestRoutes);
// Mongo Connection
mongoose.connect(
"mongodb+srv://hac:[email protected]/<dbname>?retryWrites=true&w=majority",
{
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
}
);
mongoose.connection.on("connected", () => {
console.log("Connected to mongo instance");
});
mongoose.connection.on("error", (err) => {
console.error("Error connenting to mongo", err);
});
// For Production
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
const path = require("path");
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
//Socket Com
io.on("connection", (socket) => {
socket.on("join-room", (roomId, userId) => {
socket.join(roomId);
socket.to(roomId).broadcast.emit("user-connected", userId);
socket.on("disconnect", () => {
socket.to(roomId).broadcast.emit("user-disconnected", userId);
});
});
});
const PORT = process.env.PORT || 5000;
server.listen(PORT, () => {
console.log(`Server Started on PORT ${PORT}`);
});