-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (45 loc) · 1.44 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
import express from "express";
import path from "path";
import { fileURLToPath } from "url";
import connectDB from "./config/database.js";
import postRoutes from "./routes/postRoutes.js";
import Post from "./models/post.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const port = process.env.PORT || 3000;
const app = express();
// static file 경로를 설정
app.use(express.static(path.join(__dirname, "public")));
// ejs를 사용한다고 명시를 해주고, 경로를 지정해준다.
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
// request 파싱
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.get("/", function (req, res) {
res.render("index");
});
app.use("/", postRoutes);
async function start() {
const client = await connectDB();
await Post.injectDB(client);
if (process.env.VERCEL) {
console.log("Vercel 환경에서는 서버를 시작하지 않습니다.");
} else {
app.listen(port, () => {
console.log("서버 실행중...");
});
}
process.on("SIGINT", async () => {
try {
await client.close();
console.log("정상 DB 연결 종료");
process.exit(0);
} catch (err) {
console.error("오류에 의한 DB 연결 종료", err);
process.exit(1);
}
});
}
start();
export default app;