在Ubuntu下使用Swagger主要有以下两种常见方式:
sudo apt update
sudo apt install nodejs npm
sudo npm install -g swagger-ui-express
swagger.json
或swagger.yaml
文件,定义API元数据,如路径、参数等。const express = require('express');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
const app = express();
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
const port = 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
node index.js
(假设文件名为index.js
),然后访问http://localhost:3000/api-docs
查看Swagger UI。sudo apt update
sudo apt install docker.io
docker pull swaggerapi/swagger-ui-express
docker run -p 8080:8080 -v $(pwd):/app swaggerapi/swagger-ui-express
http://localhost:8080
。