-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
65 lines (56 loc) · 1.68 KB
/
app.js
File metadata and controls
65 lines (56 loc) · 1.68 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
54
55
56
57
58
59
60
61
62
63
64
65
const express = require("express");
const { registerRoutes } = require("./src/router");
const corsMiddleware = require("./src/middleware/cors");
const { TMP_PATH, STATIC_PATH, PORT, HOST } = require("./config");
const fs = require("fs/promises");
const app = express();
app.use(corsMiddleware);
registerRoutes(app);
(async () => {
await initializeTmpFolder();
const server = app.listen(PORT, HOST, () => {
console.log(`Static server is running at http://${HOST}:${PORT}`);
console.log(`Serving static files from: ${STATIC_PATH}`);
console.log(`Temporary files are stored in: ${TMP_PATH}`);
});
server.on("close", async () => {
await cleanupTmpFolder();
});
})();
async function initializeTmpFolder() {
try {
await fs.access(TMP_PATH);
} catch {
await fs.mkdir(TMP_PATH, { recursive: true });
}
}
async function cleanupTmpFolder() {
try {
await fs.rm(TMP_PATH, { recursive: true, force: true });
console.log(`Deleted TMP folder: ${TMP_PATH}`);
} catch (err) {
if (err.code !== "ENOENT") {
console.error(`Error deleting TMP folder: ${err.message}`);
}
}
}
process.on("SIGINT", async () => {
console.log("\nShutting down server...");
await cleanupTmpFolder();
process.exit(0);
});
process.on("SIGTERM", async () => {
console.log("\nShutting down server...");
await cleanupTmpFolder();
process.exit(0);
});
process.on("uncaughtException", async (err) => {
console.error("Uncaught Exception:", err);
await cleanupTmpFolder();
process.exit(1);
});
process.on("unhandledRejection", async (reason, promise) => {
console.error("Unhandled Rejection at:", promise, "reason:", reason);
await cleanupTmpFolder();
process.exit(1);
});