在 Ubuntu 上运行 Swagger UI 的步骤如下:
-
安装 Node.js 和 npm: Swagger UI 是一个基于 Node.js 的前端库,因此你需要先安装 Node.js 和 npm。你可以使用以下命令来安装它们:
sudo apt update sudo apt install nodejs npm安装完成后,你可以通过以下命令检查它们的版本:
node -v npm -v -
安装 Swagger UI Express: Swagger UI Express 是一个可以让你轻松地在 Express 应用中集成 Swagger UI 的库。使用 npm 安装它:
npm install swagger-ui-express -
创建一个简单的 Express 应用: 创建一个新的 JavaScript 文件,例如
app.js,并添加以下代码来创建一个简单的 Express 应用:const express = require('express'); const swaggerUi = require('swagger-ui-express'); const YAML = require('yamljs'); // 读取 Swagger 文档 const swaggerDocument = YAML.load('./swagger.yaml'); const app = express(); // 使用 Swagger UI Express 中间件 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的 Swagger 文档文件。 -
创建 Swagger 文档: 创建一个名为
swagger.yaml的文件,并添加你的 API 文档。你可以手动编写 YAML 文件,或者使用 Swagger Editor 生成。以下是一个简单的示例:
swagger: '2.0' info: title: Sample API description: A sample API to demonstrate Swagger UI version: '1.0.0' paths: /users: get: summary: List all users responses: '200': description: An array of users schema: type: array items: $ref: '#/definitions/User' definitions: User: type: object properties: id: type: integer format: int64 name: type: string email: type: string format: email -
运行应用: 在终端中运行以下命令来启动你的 Express 应用:
node app.js现在,你可以在浏览器中访问
http://localhost:3000/api-docs来查看 Swagger UI。
通过这些步骤,你应该能够在 Ubuntu 上成功运行 Swagger UI。如果你遇到任何问题,请确保检查 Node.js 和 npm 的安装是否正确,并查看应用的日志以获取更多信息。
以上就是关于“如何在ubuntu上运行swagger UI”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm