0

我正在尝试为我的 Node JS express API 创建招摇的文档。在 app.js 中添加以下代码:

const options = {
  definition: {
    openapi: '3.0.0', // Specification (optional, defaults to swagger: '2.0')
    info: {
      title: 'Hello World!', // Title (required)
      version: '1.0.0', // Version (required)
    },
  },
  // Path to the API docs
  apis: ['routes/*.js']
};
const swaggerSpec = swaggerJSDoc(options);
// console.log(swaggerSpec)
app.get('/api-docs.json', (req, res) => {
  res.setHeader('Content-Type', 'application/json');
  res.send(swaggerSpec);
});
app.use('/api/docs',express.static('./node_modules/swagger-ui/dist'));

在其中一种申请途径中添加了以下文档:

/**
 * @swagger
 * resourcePath: /api
 * /login:
 *   post:
 *     description: Login to the application
 *     produces:
 *       - application/json
 *     parameters:
 *       - name: username
 *         description: Username to use for login.
 *         in: formData
 *         required: true
 *         type: string
 *       - name: password
 *         description: User's password.
 *         in: formData
 *         required: true
 *         type: string
 *     responses:
 *       200:
 *         description: login
 */

访问http://localhost:4000/api-docs.json时生成以下输出

{"openapi":"3.0.0","info":{"title":"Hello World!","version":"1.0.0"},"paths":{},"components":{},"tags":[]}

但是,不会生成实际文档。任何解决此问题的帮助将不胜感激。

4

1 回答 1

1

找出问题及其解决方案。

apis 属性的路径必须是从根修改 apis: ['routes/*.js']到的相对路径,apis: ["src/routers/*.js"]并且它有效。

于 2020-07-05T13:46:49.623 回答