以下是在Linux上使用Swagger进行API模拟的步骤:
sudo npm install -g swagger-ui-express
;用Docker安装时,执行docker pull swaggerapi/swagger-ui-express
。swagger.json
或swagger.yaml
文件,定义API规范,如端点、参数、请求和响应等。swagger-ui-express
和swagger-jsdoc
,在代码中引入并配置,如const swaggerUi = require('swagger-ui-express'); const swaggerDocument = require('./swagger.json'); app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
。node index.js
,或启动Docker容器docker run -p 8080:8080 -e SWAGGER_JSON=/app/swagger.json -v $(pwd):/app swaggerapi/swagger-ui-express
。http://localhost:3000/api-docs
(端口可能因配置不同而有差异)。