-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (54 loc) · 2.25 KB
/
index.js
File metadata and controls
66 lines (54 loc) · 2.25 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
import expressConfig from "./config/expressConfig.js";
import { startSchedulers } from "./config/scheduler.js";
import { status } from "./config/responseStatus.js";
import { BaseError } from "./config/error.js";
import { response } from "./config/response.js";
import { specs } from "./config/swaggerConfig.js";
import SwaggerUi from "swagger-ui-express";
import { userRouter } from "./src/app/User/userRoute.js";
import { pcapsuleRouter } from "./src/app/Pcapsule/pcapsuleRoute.js";
import { rcapsuleRouter } from "./src/app/Rcapsule/rcapsuleRoute.js";
import { healthRoute } from "./src/app/Health/healthRoute.js";
import { capsuleRouter } from "./src/app/Capsule/capsuleRoute.js";
const app = expressConfig();
const port = 3000;
// swagger
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("/user", userRouter);
app.use("/capsule", capsuleRouter);
app.use("/pcapsule", pcapsuleRouter);
app.use("/rcapsule", rcapsuleRouter);
startSchedulers();
// 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);
// 에러 데이터가 있는 경우에만 해당 데이터를 사용하여 응답을 보냄
if (err.data) {
// BaseError 인스턴스인지 확인, 그에 맞는 HTTP 상태 코드를 설정 후 응답
if (err instanceof BaseError) {
const { status, isSuccess, code, message } = err.data;
res.status(status).send(response({ isSuccess, code, message }));
} else {
// BaseError가 아닌 다른 에러인 경우에는 기본적으로 500 상태 코드를 반환
res.status(500).send(response(status.INTERNAL_SERVER_ERROR));
}
} else {
// 에러 데이터가 없는 경우에는 기본적으로 500 상태 코드를 반환
res.status(500).send(response(status.INTERNAL_SERVER_ERROR));
}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});