我试图使用swagger为我的express项目生成文档,但它没有呈现。我在为文档配置的url中加载了以下消息:
Unable to render this definition
The provided definition does not specify a valid version field.
Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.x.y (for example, openapi: 3.1.0).
字符串
这是我配置文档的app.js
import express from "express";
import mongoose from "mongoose";
import {routes} from './src/routes/routes.js';
//swagger
import swaggerUI from "swagger-ui-express";
const swaggerSpec = {
definition: {
swagger: "2.0",
openapi: "3.0.3",
info: {
title:"Playlist API",
version:"1.0.0"
},
servers:{
url: "http://localhost:3000"
},
paths: routes
}
}
const app = express();
app.use(express.json());
app.use("/api-doc", swaggerUI.serve, swaggerUI.setup(swaggerSpec));
app.use('', routes); // Prefijo '/api'
const port = 3000;
mongoose.connect("mongodb://127.0.0.1:27017/playlist-multi-schema", { useNewUrlParser: true });
app.listen(port, () => {
console.log(`Iniciado en puerto ${port}`)
});
型
这是我的routes.js:
import express from 'express';
const router = express.Router();
import { Genero } from "../models/generoModel.js"
import { Cancion } from "../models/cancionModel.js"
router.get("/api/generos", async (req, res) => {
res.send(await Genero.find());
});
router.put("/api/generos/:generoAnterior", async (req, res) => {
await Genero.updateOne({genero: req.params.generoAnterior}, {genero: req.body.nuevoGenero});
res.send(await Genero.find({genero: req.body.generoNuevo}));
});
router.delete("/api/generos/:genero", async(req,res)=>{
let borrado = await Genero.findOneAndDelete({genero: req.params.genero});;
res.send(borrado);
})
// Exporta el enrutador
export const routes = router;
型
1条答案
按热度按时间mum43rcc1#
我不知道你在哪里找到
definition
属性,但我不相信它是swagger-ui-express包中的预期属性。试试这个
个字符
如果你导航到https://localhost:3000/hello,它就能工作。