Chapter 8. 프론트엔드 연동과 Swagger

Arin·2025년 12월 26일

UMC 8기 - Node.js

목록 보기
10/11

swagger 관련 라이브러리 설치

npm add swagger-autogen swagger-ui-express

swagger 세팅

import swaggerAutogen from "swagger-autogen" 
import swaggerUiExpress from "swagger-ui-express"

app.use(
  "/docs",
  swaggerUiExpress.serve,
  swaggerUiExpress.setup({}, {
    swaggerOptions: {
      url: "/openapi.json",
    },
  })
);

app.get("/openapi.json", async (req, res, next) => {
  // #swagger.ignore = true
  const options = {
    openapi: "3.0.0",
    disableLogs: true,
    writeOutputFile: false,
  };
  const outputFile = "/dev/null"; // 파일 출력은 사용하지 않습니다.
  const routes = ["./src/index.js"];
  const doc = {
    info: {
      title: "UMC 7th",
      description: "UMC 7th Node.js 테스트 프로젝트입니다.",
    },
    host: "localhost:3000",
  };

  const result = await swaggerAutogen(options)(outputFile, routes, doc);
  res.json(result ? result.data : null);
});


profile
헤맨만큼이 내 땅이다

0개의 댓글