规范中未定义任何操作! -ES6节点Express Swagger

时间:2020-06-11 11:36:25

标签: node.js express ecmascript-6 swagger

我已经从存储库中克隆了一个可用的API,并尝试配置Swagger(openAPI)。

1)没有Swagger的原始文件( npm run dev ): https://github.com/FaztWeb/nodejs-sequelize-rest-api.git

2)我的Swagger克隆( npm run dev ): https://github.com/djgaston/node-express-swagger.git

该堆栈包含 Node.js + Express + SQLite3 ,但使用 javascript ES6 ,这使路由器方法变得复杂。此外,它使用委托https://www.npmjs.com/package/consign)和 Sequelize https://www.npmjs.com/package/sequelize

我无法让Swagger拾取属性“ apis:['./routes/*.js']”中定义的所有路由。

当我运行项目时,swagger文档包含一条消息,内容为“规范中未定义操作!” ,并且swagger.json文件确认未找到文件。

{
"info": {
"title": "my",
"description": "my API",
"contact": {
"name": "me"
},
"servers": [
"http://localhost:3000"
]
},
"swagger": "2.0",
"paths": {},
"definitions": {},
"responses": {},
"parameters": {},
"securityDefinitions": {},
"tags": []
}

这是我修改的代码: index.js

const express = require('express');
const app = express();
const consign = require('consign');

const swaggerJsDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const bodyParser = require('body-parser');

const port = process.env.PORT || 3000;

const swaggerOptions = {
    swaggerDefinition: {
        info: {
            title: 'my',
            description: 'my API',
            contact: {
                name: 'me'
            },
            servers: ['http://localhost:' + port]
        }
    },
    swagger: '2.0',
    basePath: '/v1',
    schemes: [
        'http',
        'https'
    ],
    consumes: [
        'application/json'
    ],
    produces: [
        'application/json'
    ],
    apis: ['./routes/*.js']
}

// Routes
consign({cwd: __dirname})
  .include('libs/config.js')
  .then('db.js')
  .then('libs/middlewares.js')
  .then('routes')
  .then('libs/boot.js')
  .into(app);

const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use(bodyParser.json());
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs))

// serve swagger (http://localhost:3000/swagger.json)
app.get('/swagger.json', function(req, res) {
    res.setHeader('Content-Type', 'application/json');
    res.send(swaggerDocs);
});

tasks.js

module.exports = app => {

  const Task = app.db.models.Task;

    /**
    * @swagger
    * /tasks:
    *  get:
    *    description: Use to request all customers
    *    responses:
    *      '200':
    *        description: A successful response
    */
    app.route('/v1/tasks').get((req, res) => {
      Task.findAll({})
        .then(result => res.json(result))
        .catch(error => {
          res.status(412).json({msg: error.message});
        });
    });

    /**
    * @swagger
    * /tasks:
    *  post:
    *    description: Use to insert the json content
    *    responses:
    *      '200':
    *        description: A successful response
    */
    app.route('/v1/tasks').post((req, res) => {
      Task.create(req.body)
        .then(result => res.json(result))
        .catch(error => {
          res.status(412).json({msg: error.message});
        });
    });

    /**
    * @swagger
    * /tasks/{id}:
    *  get:
    *    description: Use to request one customer
    *    responses:
    *      '200':
    *        description: A successful response
    */
    app.route('/v1/tasks/:id').get((req, res) => {
      Task.findOne({where: req.params})
        .then(result => {
          if (result) {
            res.json(result);
          } else {
            res.sendStatus(404);
          }
        })
        .catch(error => {
          res.status(412).json({msg: error.message});
        });
    });

    /**
    * @swagger
    * /tasks/{id}:
    *  put:
    *    description: Use to update a record with the json content
    *    responses:
    *      '200':
    *        description: A successful response
    */
    app.route('/v1/tasks/:id').put((req, res) => {
      Task.update(req.body, {where: req.params})
        .then(result => res.sendStatus(204))
        .catch(error => {
          res.status(412).json({msg: error.message});
        });
    });

    /**
    * @swagger
    * /tasks/{id}:
    *  delete:
    *    description: Use to delete a record
    *    responses:
    *      '200':
    *        description: A successful response
    */
    app.route('/v1/tasks/:id').delete((req, res) => {
      Task.destroy({where: req.params})
        .then(result => res.sendStatus(204))
        .catch(error => {
          res.status(204).json({msg: error.message});
        });
    });

};

0 个答案:

没有答案