-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
67 lines (52 loc) · 1.61 KB
/
index.js
File metadata and controls
67 lines (52 loc) · 1.61 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
66
67
// index.js
import { PrismaClient } from "@prisma/client";
import express from "express";
import { authRoutes } from "./routes/MainAuth.js";
import { postsRoutes } from "./routes/MainPosts.js";
import cookieParser from "cookie-parser";
import cors from "cors";
import dotenv from "dotenv";
dotenv.config();
const app = express();
const prisma = new PrismaClient();
app.use(
cors({
origin: process.env.FRONTEND_URL || "http://localhost:5173",
credentials: true,
}),
);
app.use(express.json({ limit: "10mb" }));
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser(process.env.COOKIE_SECRET || "yourSecretHere"));
app.use("/api/auth", authRoutes);
app.use("/api", postsRoutes);
// *, specifies that if there are no routes
app.use("*", (req, res) => {
res.status(404).json({
status: "error",
message: "Route not found",
code: 404,
});
});
const PORT = process.env.PORT || 3000;
const server = app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
const gracefulShutdown = async (signal) => {
console.log(`Received ${signal}. Shutting down gracefully...`);
server.close(() => {
console.log("HTTP server closed.");
});
await prisma.$disconnect();
console.log("Database connection closed.");
process.exit(0);
};
process.on("SIGINT", () => gracefulShutdown("SIGINT"));
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
process.on("unhandledRejection", (reason, promise) => {
console.error("Unhandled Rejection at:", promise, "reason:", reason);
});
process.on("uncaughtException", (error) => {
console.error("Uncaught Exception:", error);
process.exit(1);
});