-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (92 loc) · 2.53 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
require("dotenv").config();
const mongoose = require("mongoose");
const morgan = require("morgan");
const path = require("path");
const express = require("express");
const { DataBase, MongoAuth, MongoUser } = require("./envVariables.js");
const ProxyModule = require("./proxyServer.js");
const { router } = require("./routes/userRoute.js");
process.on("uncaughtException", (err) => {
console.log("UNCAUGHT_EXCEPTION! Shutting down...");
console.log(err.name, err.message);
process.exit(1);
});
// Create Express Server
const app = express();
app.use(
express.urlencoded({
extended: true,
})
);
app.use(express.json());
// Serve static files from the React app
app.use(express.static(path.join(__dirname, "clientv2/public")));
// Dashboard Port
const PORT = 5000;
// Logging
app.use(morgan("dev"));
const dashboardServer = () => {
const instanceOfServer = app.listen(PORT, () => {
// User Specific EndPoints
app.use("/api/v1/users", router);
app.get("*", (req, res, next) => {
res.sendFile(path.join(__dirname + "/clientv2/public/auth/login.html"));
});
console.log(
"%c%s",
"color: green;",
`Dashboard Server Running on http://localhost:${PORT} \n`
);
});
instanceOfServer.on("close", () => {
mongoose.connection.close();
console.log("server shutting down");
});
return instanceOfServer;
};
// for being ensure everything is captured first we connect the mongo DB
// then our proxy!
const dbConnection = () => {
return mongoose
.connect(DataBase, {
auth: {
user: MongoUser,
password: MongoAuth,
},
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
poolSize: 10,
})
.then(() => {
console.log("%c%s", "color: green;", "DB connection successful! \n");
dashboardServer();
})
.catch((e) => {
console.error(
"Failed to connect to mongo on startup - retrying in 5 sec",
e
);
mongoose.connection.close();
dashboardServer().close();
setTimeout(dbConnection, 5000);
});
};
mongoose.connection.on("close", () => {
console.log("DB Disconnected Successfully");
});
process.on("SIGINT", () => {
mongoose.connection.close().then(() => {
process.exit(1);
});
});
process.on("unhandledRejection", (err) => {
console.log(err.name, err.message);
console.log("UNHANDLED_REJECTION! Shutting down...");
dashboardServer.close(() => {
process.exit(1);
});
});
dbConnection();
ProxyModule.ProxyServer();