在Ubuntu上使用Swagger调试代码,通常涉及以下几个步骤:
sudo apt update
sudo apt install nodejs npm
sudo npm install -g swagger-ui-express
app.js
的文件,并添加以下内容:const express = require('express');
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
// Load Swagger document
const swaggerDocument = YAML.load('./swagger.yaml');
const app = express();
// Serve Swagger docs
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
swagger.yaml
的文件,并添加你的API文档。node app.js
http://localhost:3000/api-docs
来查看Swagger UI界面。docker run --name swagger-validator -d -p 8189:8080 --add-host test.local:10.0.75.1 swaggerapi/swagger-validator
swagger-cli validate http://localhost:8189/debug?url=http://test.local:8002/swagger/?format=openapi
通过以上步骤,你应该能够在Ubuntu上成功安装和配置Swagger,并进行API文档的生成和测试。如果在安装过程中遇到问题,可以参考相关的官方文档或社区论坛寻求帮助。