-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
43 lines (35 loc) · 1.09 KB
/
Copy pathserver.js
File metadata and controls
43 lines (35 loc) · 1.09 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
import mongoose from "mongoose";
import app from "./app.js";
import { connectDatabase } from "./config/database.js";
import env from "./config/env.js";
let server;
const shutdown = async (signal, exitCode = 0) => {
console.log(`${signal} received. Shutting down gracefully.`);
if (server) {
await new Promise((resolve) => server.close(resolve));
}
await mongoose.connection.close();
process.exit(exitCode);
};
const startServer = async () => {
try {
await connectDatabase();
server = app.listen(env.port, () => {
console.log(`EventSphere API running on port ${env.port}`);
});
} catch (error) {
console.error("Unable to start EventSphere API:", error.message);
process.exit(1);
}
};
process.on("SIGINT", () => shutdown("SIGINT"));
process.on("SIGTERM", () => shutdown("SIGTERM"));
process.on("unhandledRejection", (error) => {
console.error("Unhandled rejection:", error);
shutdown("unhandledRejection", 1);
});
process.on("uncaughtException", (error) => {
console.error("Uncaught exception:", error);
shutdown("uncaughtException", 1);
});
startServer();