-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
53 lines (45 loc) · 1.48 KB
/
server.js
File metadata and controls
53 lines (45 loc) · 1.48 KB
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
import mongoose from "mongoose";
import config from "./config/config.js";
process.on("uncaughtException", (err) => {
console.log("UNCAUGHT EXCEPTION! 💥 Shutting down...");
console.log(err.name, err.message);
process.exit(1);
});
import app from "./app.js";
const DB = config.DB_URL.replace("<password>", config.DB_PASSWORD);
// DB connection
const options = {
// autoIndex: false, // Don't build indexes
maxPoolSize: 10, // Maintain up to 10 socket connections
serverSelectionTimeoutMS: 10000, // Keep trying to send operations for 5 seconds
// socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
// family: 4, // Use IPv4, skip trying IPv6
useUnifiedTopology: true,
useNewUrlParser: true,
};
mongoose
.connect(DB, options)
.then(() => {
console.log("DB connected successfully!");
})
.catch((err) => console.log(err));
const port = process.env.PORT || config.PORT || 8080;
const ENV = process.env.NODE_ENV || "prod";
const server = app.listen(port, () => {
console.log(
`Server started on PORT: ${port} in ${ENV.trim().toUpperCase()} mode`
);
});
process.on("unhandledRejection", (err) => {
console.log("UNHANDLED REJECTION! 💥 Shutting down...");
console.log(err.name, err.message);
server.close(() => {
process.exit(1);
});
});
process.on("SIGTERM", () => {
console.log("👋 SIGTERM RECEIVED. Shutting down gracefully");
server.close(() => {
console.log("💥 Process terminated!");
});
});