-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
75 lines (61 loc) · 1.92 KB
/
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
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
66
67
68
69
70
71
72
73
74
75
import express from "express";
import eventRoutes from "./routes/event.routes.js";
import photoRoutes from "./routes/photo.routes.js";
import podcastRoutes from "./routes/podcast.routes.js";
import memberRoutes from "./routes/member.routes.js";
import { getCaptcha } from "./controllers/captcha.controller.js";
import { logging } from "./middlewares/logger.js";
import errorHandler from "./errors/errorHandler.js";
import { ClientError, ServerError } from "./errors/ApiError.js";
import { fileURLToPath } from "url";
import path from "path";
import { securityMiddleware } from "./middlewares/security.js";
import dotenv from "dotenv";
dotenv.config();
console.log("Created by Vishal and Samuel Jabez");
let app = express();
app.use(logging);
app.use(securityMiddleware());
app.use("/static", express.static("static"));
app.use("/api/events", eventRoutes);
app.use("/api/podcasts", podcastRoutes);
app.use("/api/members", memberRoutes);
app.get("/api/credits", (req, res) => {
res.sendFile("credits.json", {
root: path.dirname(fileURLToPath(import.meta.url)),
});
});
app.post("/api/captcha", express.json(), getCaptcha);
app.use("/images/are/not/here", photoRoutes);
app.use((req, res) => {
throw ClientError.notFound();
});
app.use((req, res) => {
throw ServerError.notImplemented();
});
// Centralized Error Handler
app.use(errorHandler);
const server = app.listen(3000, () => {
console.log("server start in http://localhost:3000");
});
const cleaner = () => {
server.close(async () => {
(await database()).close();
});
};
process.on("SIGINT", cleaner);
process.on("SIGTERM", cleaner);
process.on("uncaughtException", (error) => {
console.error(
"Uncaught Exception:",
error instanceof Error ? error.message : String(error)
);
cleaner();
});
process.on("unhandledRejection", (error) => {
console.error(
"Unhandled Rejection:",
error instanceof Error ? error.message : String(error)
);
cleaner();
});