在Ubuntu系统下运行Swagger,你需要遵循以下步骤:
-
安装Node.js和npm(如果尚未安装): 打开终端,运行以下命令来安装Node.js和npm:
sudo apt update sudo apt install nodejs npm -
安装Swagger UI: 使用npm全局安装Swagger UI:
sudo npm install -g swagger-ui-express -
创建一个简单的Express应用(如果你还没有一个): 在你的项目目录中,创建一个名为
app.js的文件,并添加以下内容:const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`); }); -
在Express应用中集成Swagger UI: 修改
app.js文件,添加Swagger UI中间件和配置:const express = require('express'); const swaggerUi = require('swagger-ui-express'); const YAML = require('yamljs'); const swaggerDocument = YAML.load('./swagger.yaml'); // 确保你有一个swagger.yaml文件 const app = express(); const port = 3000; app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`); }); -
创建Swagger规范文件(swagger.yaml): 在你的项目目录中创建一个名为
swagger.yaml的文件,并添加你的API规范。例如:swagger: '2.0' info: title: Sample API description: A sample API to demonstrate Swagger integration version: '1.0.0' host: localhost:3000 basePath: / schemes: - http paths: /: get: summary: Returns a hello message responses: '200': description: A successful response schema: type: string -
运行你的Express应用: 在终端中,导航到你的项目目录并运行以下命令:
node app.js -
访问Swagger UI: 打开浏览器并访问
http://localhost:3000/api-docs,你应该能看到Swagger UI界面,其中包含了你的API文档。
请确保你已经安装了所有必要的依赖,并且你的API规范文件(swagger.yaml)是正确的。如果有任何问题,请检查错误消息并进行相应的调整。
以上就是关于“ubuntu下swagger如何运行”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm