-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (42 loc) · 1.32 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
import express from "express";
import path from "path";
import postRoutes from "./routes/postRoutes.js";
import connectDB from "./config/database.js";
import Post from "./models/post.js";
import { fileURLToPath } from "url";
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.use(express.static(path.join(__dirname, "public")));
app.set("view engine", "ejs");
app.set('views', path.join(__dirname, 'views'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
const port = process.env.PORT || 3000;
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;