-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
82 lines (71 loc) ยท 2.21 KB
/
index.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
76
77
78
79
80
81
82
require("dotenv").config();
const { swaggerUi, specs } = require("./swagger");
const express = require("express");
const cors = require("cors");
const cookieParser = require("cookie-parser");
const mongoose = require("mongoose");
const MongoStore = require("connect-mongo");
const session = require("express-session");
const app = express();
const port = process.env.PORT || 8000;
// MongoDB ์ฐ๊ฒฐ ์ค์
const connectDB = async () => {
try {
const conn = await mongoose.connect(process.env.MONGO_URI, {
dbName: "devporest", // ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ด๋ฆ์ 'devporest'๋ก ์ค์ (default: 'test')
});
console.log(`MongoDB ์ฐ๊ฒฐ ์ฑ๊ณต: ${conn.connection.host}`);
} catch (error) {
console.error("MongoDB ์ฐ๊ฒฐ ์คํจ:", error);
process.exit(1);
}
};
// MongoDB ์ฐ๊ฒฐ
connectDB();
// ๋ฏธ๋ค์จ์ด ์ค์
app.use(
cors({
origin: [
process.env.CORS_ORIGIN,
process.env.SERVER_DEPLOY_URL,
"http://localhost:5173",
"http://localhost:8000",
],
credentials: true,
})
);
app.use(express.json()); // JSON ํ์ฑ
app.use(cookieParser()); // ์ฟ ํค ํ์ฑ
app.use(express.json({ limit: "7mb" }));
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(specs)); // Swagger UI๋ฅผ /api-docs ๊ฒฝ๋ก์ ๋ผ์ฐํ
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
store: MongoStore.create({
mongoUrl: process.env.MONGO_URI,
ttl: 24 * 60 * 60, // ์ธ์
์ ํจ๊ธฐ๊ฐ 1์ผ
}),
cookie: {
secure: process.env.NODE_ENV === "production", // HTTPS (๋ณด์ ์ฐ๊ฒฐ)์์๋ง ์ฟ ํค๊ฐ ์ ์ก๋จ
maxAge: 24 * 60 * 60 * 1000, // 24์๊ฐ
},
})
);
// ๊ธฐ๋ณธ ๋ผ์ฐํธ ์ค์
app.get("/", (req, res) => {
res.send("Express ์๋ฒ๊ฐ ์คํ ์ค์
๋๋ค.");
});
// API ๋ผ์ฐํธ ์ค์
const apiRoutes = require("./routes");
app.use("/api", apiRoutes);
// ์๋ฒ ์์
app.listen(port, () => {
console.log(`์๋ฒ๊ฐ ${port}๋ฒ ํฌํธ์์ ์คํ ์ค์
๋๋ค...`);
console.log(`CORS origin: ${process.env.CORS_ORIGIN}`);
});
// ์กด์ฌํ์ง ์๋ ๋ผ์ฐํธ์ ๋ํ ์ฒ๋ฆฌ
app.use("*", (req, res) => {
res.status(404).json({ message: "์์ฒญํ์ ํ์ด์ง๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค." });
});