我尝试使用swagger创建有关现有API的文档,但是当我使用openapi时,此错误:无法读取未定义的属性“ get”显示在控制台中。
当我使用openapi: "3.0.0"
app.js代码:
const swaggerUi = require('swagger-ui-express'), swaggerDocument = require('./swagger.json');
const swaggerJsdoc = require('swagger-jsdoc');
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const handlebars = require('express-handlebars');
const helmet = require('helmet');
const app = express();//Here define app for create a middlewares using app.use();
const swaggerOptions = {
swaggerDefinition: {
info: {
title: 'Ágil API',
version: '1.0.0',
description: 'Document API with autogenerated swagger doc',
},
host: 'localhost:3000',
basePath: '/',
components: {
securitySchemes: {
BasicAuth: {
type: "http",
scheme: "basic"
}
}
},
security: {
basicAuth: []
},
openapi: "3.0.0",
},
apis: ['./routes/v1/user.js'],
}
const swaggerSpecs = swaggerJsdoc(swaggerOptions);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpecs));
进入用户路径,我输入以下代码:
const express = require('express');
const router = express.Router();
const controller = require('../../controllers/v1/user-controller');
/**
* @swagger
* definition:
* login:
* properties:
* username:
* type: string
* password:
* type: string
*/
/**
* @swagger
* /user/login:
* post:
* tags:
* - User
* description: User login
* produces:
* - application/x-www-form-urlencoded
* parameters:
* - name: body
* description: login object
* in: body
* required: true
* schema:
* $ref: '#/definitions/login'
* - name: version
* description: sistem version
* in: header
* required: true
* responses:
* 200:
* description: "token"
* 400:
* description: Invalid credentials
*/
router.post('/login', controller.login);