-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
38 lines (32 loc) · 1005 Bytes
/
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
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const http = require("http");
const app = require("./app");
// * Uncaught Exception Error
process.on("uncaughtException", (err) => {
console.error(`${err.name}. Server is shutting down.`);
console.error(err.message);
});
// * Configuration
dotenv.config({ path: "./config.env" });
// * MongoDB connection
(async () => {
try {
await mongoose.connect(process.env.URI);
console.log("Connecting to the database is successful.");
} catch (e) {
console.error(`Connecting to the database is failed. ${e}`);
}
})();
// * Creating the server
const server = http.createServer(app);
// * Listening to the server
server.listen(process.env.PORT, () =>
console.log(`Server is running on PORT: ${process.env.PORT}`)
);
// * Unhandled Rejection
process.on("unhandledRejection", (err) => {
console.error(`${err.name} Server is shutting down.`);
console.error(err.message);
server.close(() => process.exit(1));
});