원래는 Swagger로 API들을 관리하고 있었는데, UI가 보기 불편하다는 생각을 종종하였다.
대체 할수있는 것들이 뭐가 있을까 고민하던 중 Redoc에 대해 알게 되었고 적용하였다.
심지어 현재 openapi 3.0으로 작성된 yaml 파일이 있다면 코드 몇줄만 추가하면 별도로 다시 yaml 파일을 작성할 필요도 없다!
📌 설치
npm install redoc-express
📌 Redoc Express 적용
import redoc from "redoc-express";
app.get("/redoc", redoc({
title: "API Documentation",
specUrl: "/swagger.yaml",
expandResponses: "200,201",
}));
Server 부분 코드
app.get("/redoc", redoc({
title: "API Documentation",
specUrl: "/swagger.yaml",
redocOptions: {
expandResponses: "200,201", // 200, 201 응답을 기본 확장
hideDownloadButton: true, // JSON 다운로드 버튼 숨김 (선택)
theme: {
colors: {
primary: { main: "#007bff" }, // 기본 색상 설정 (선택)
},
typography: {
fontSize: "16px",
},
},
}
}));
// OpenAPI YAML 파일 제공
app.get("/swagger.yaml", (req, res) => {
res.type("application/yaml").sendFile(path.join(__dirname, "본인 swagger 파일 경로.yaml"));
});
아래와 같이 적용된것을 확인!
유로플랜을 쓰지 않는 이상 대신 직접 endpoint로 호출테스트는 불가능 하기 때문에
swagger와 같이 쓰는 것을 추천합니다!
감사합니다.
'NodeJS' 카테고리의 다른 글
[TypeScript] Interface 와 Type 의 차이 (0) | 2024.11.14 |
---|---|
NodeJS 엔터프라이즈 어플리케이션의 9가지 원칙 - 1편 (9) | 2024.11.05 |