-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
32 lines (25 loc) · 814 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
const dotenv = require("dotenv");
const app = require("./app");
const mongoose = require("mongoose");
dotenv.config({ path: "./config.env" });
process.on("uncaughtException", (err) => {
console.error(`${err.name} - Server is shutting down.`);
console.error(err.message);
process.exit(1);
});
(async () => {
try {
await mongoose.connect(process.env.URI);
console.log("Connecting to the database successfully.");
} catch (e) {
console.error(`Error connecting to the database: ${e}`);
}
})();
const server = app.listen(process.env.PORT, () =>
console.log(`Server is running on PORT: ${process.env.PORT}`)
);
process.on("unhandledRejection", (err) => {
console.error(`${err.name} - Server is shutting down.`);
console.error(err.message);
server.close(() => process.exit(1));
});