-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (49 loc) · 1.99 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
/* //////////////////////////////////////////////////
ENVIRONMENT VARIABLES
////////////////////////////////////////////////// */
const dotenv = require("dotenv");
dotenv.config({ path: "./config.env" });
/* //////////////////////////////////////////////////
MONGOOSE
////////////////////////////////////////////////// */
const mongoose = require("mongoose");
/* //////////////////////////////////////////////////
APP
////////////////////////////////////////////////// */
const app = require("./app");
/* //////////////////////////////////////////////////
DATABASE CONNECTION
////////////////////////////////////////////////// */
const DB = process.env.DATABASE.replace("<PASSWORD>", process.env.DATABASE_PASSWORD);
dbConnect().catch(err => console.log(err));
async function dbConnect() {
await mongoose.connect(DB).then(con => {
// console.log(con.connections);
console.log("🔌🔌🔌Database connection successful🔌🔌🔌")
});
}
/* //////////////////////////////////////////////////
SERVER START & LISTEN
////////////////////////////////////////////////// */
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`App is running on port ${port}`);
});
/* //////////////////////////////////////////////////
UNHANDLED REJECTIONS
////////////////////////////////////////////////// */
process.on("unhandledRejection", err => {
console.error("Unhandled rejection detected:", err);
server.close(() => {
/* We first close the server gracefully, then exit process, that way the server
can finish all the request that are still pending or being handled*/
process.exit(1); //1 is code for uncaught exceptions, 0 is for success
});
});
/* //////////////////////////////////////////////////
UNCAUGHT EXCEPTIONS
////////////////////////////////////////////////// */
process.on("uncaughtException", err => {
console.error("Unhandled rejection detected:", err);
process.exit(1); //1 is code for uncaught exceptions, 0 is for success
});