【问题标题】:API endpoint documentation no appearing using swagger and express使用 swagger 和 express 没有出现 API 端点文档
【发布时间】:2019-12-25 20:11:20
【问题描述】:

我正在使用 Swagger 编写我的 API 文档。我已经在我的 express 项目上成功设置了 Swagger,但是端点的文档不会显示在除了 app.js 文件之外的另一个路由文件中

下面是我的 App.js 文件

const express = require('express')
const port = process.env.PORT
const userRouter = require('./routers/user')
require('./db/db')

const app = express()
const swaggerJsDoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");

// Extended: https://swagger.io/specification/#infoObject
const swaggerOptions = {
    swaggerDefinition: {
        info: {
            title: "MY API TITLE",
            description: "API Documentation",
            contact: {
                name: "Cali"
            },
            servers: ["http://localhost:3000"]
        }
    },
    // ['.routes/*.js']
    apis: ["app.js"]
};

const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use("/api/doc", swaggerUi.serve, swaggerUi.setup(swaggerDocs, {explorer: true}));

app.use(express.json())
app.use(userRouter)

app.listen(port, () => {
    console.log(`Server running on port ${port}`)
})

下面是我的用户路由文件

const express = require('express')
const User = require('../models/User')
const auth = require('../middleware/auth')

const router = express.Router()

const swaggerJsdoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");


router.use('/api-docs', swaggerUi.serve);
router.get('/api-docs', swaggerUi.setup(swaggerJsdoc));


/**
 * @swagger
 * /customers:
 *  get:
 *    description: Use to request all customers
 *    responses:
 *      '200':
 *        description: A successful response..
 */
router.get('/customers', (req, res) => {
    res.status(200).send('Welcome to Customers page');
})

我做错了什么? PS:我是 Swagger 的新手

【问题讨论】:

    标签: node.js express swagger swagger-ui


    【解决方案1】:

    您找到问题的答案了吗?

    我正在与一些类似的情况作斗争,我已经意识到这一点:

    // ['.routes/*.js']
        apis: ["app.js"]
    

    您需要指定所有 js 路由文件所在的绝对路径。你也可以使用正则表达式!

    我的项目结构是:

    -/src
    --/app
    ----/routes
    ------login.route.js (here is my **login route** with the @swagger notation)
    ------user.route.js (here is my **user route** with the @swagger notation)
    

    所以我的正则表达式将是

    apis: ['./src/app/routes/**.js'],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 2014-06-17
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      相关资源
      最近更新 更多