-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (44 loc) · 1.89 KB
/
index.js
File metadata and controls
56 lines (44 loc) · 1.89 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
import express from "express";
import { specs } from "./config/swagger.config.js";
import SwaggerUi from "swagger-ui-express";
import dotenv from "dotenv";
import cors from "cors";
import { response } from "./config/response.js";
import { BaseError } from "./config/error.js";
import { status } from "./config/response.status.js";
import { healthRoute } from "./src/routes/health.route.js";
import { authRouter } from "./src/routes/auth.routes.js";
import { feedbackRoute } from './src/routes/feedback.route.js';
dotenv.config(); // .env 파일 사용 (환경 변수 관리)
const app = express();
// server setting - veiw, static, body-parser etc..
app.set("port", process.env.PORT || 3000); // 서버 포트 지정
app.use(cors()); // cors 방식 허용
app.use(express.static("public")); // 정적 파일 접근
app.use(express.json()); // request의 본문을 json으로 해석할 수 있도록 함 (JSON 형태의 요청 body를 파싱하기 위함)
app.use(express.urlencoded({ extended: false })); // 단순 객체 문자열 형태로 본문 데이터 해석
app.use("/api-docs", SwaggerUi.serve, SwaggerUi.setup(specs));
app.use("/health", healthRoute);
app.get("/", (req, res, next) => {
res.send(response(status.SUCCESS, "루트 페이지!"));
});
app.use("/api", authRouter);
app.use('/feedback',feedbackRoute);
// error handling
app.use((req, res, next) => {
const err = new BaseError(status.NOT_FOUND);
next(err);
});
app.use((err, req, res, next) => {
// 템플릿 엔진 변수 설정
res.locals.message = err.message;
// 개발환경이면 에러를 출력하고 아니면 출력하지 않기
res.locals.error = process.env.NODE_ENV !== "production" ? err : {};
console.error(err);
res
.status(err.data.status || status.INTERNAL_SERVER_ERROR)
.send(response(err.data));
});
app.listen(app.get("port"), () => {
console.log(`Example app listening on port ${app.get("port")}`);
});