在Linux系统中集成Swagger UI通常涉及以下几个步骤:
-
安装Node.js和npm: Swagger UI是一个基于Node.js的应用程序,因此首先需要在你的Linux系统上安装Node.js和npm(Node.js的包管理器)。
sudo apt update sudo apt install nodejs npm或者使用NodeSource的二进制分发库来安装特定版本的Node.js:
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash - sudo apt-get install -y nodejs -
安装Swagger UI Express: Swagger UI Express是一个可以与你的Express应用程序集成的Swagger UI中间件。在你的项目目录中,运行以下命令来安装它:
npm install swagger-ui-express -
设置Swagger文档: 你需要定义一个Swagger文档,通常是一个YAML或JSON文件,描述了你的API接口。这个文件通常被称为
swagger.json。swagger: '2.0' info: title: Sample API description: A sample API to demonstrate Swagger integration 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 required: - id - name -
集成Swagger UI到Express应用: 在你的Express应用程序中,引入Swagger UI Express并配置它以使用你的Swagger文档。
const express = require('express'); const swaggerUi = require('swagger-ui-express'); const YAML = require('yamljs'); const app = express(); const swaggerDocument = YAML.load('./swagger.json'); 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}`); }); -
运行你的Express应用程序: 使用npm启动你的Express应用程序。
node app.js现在,你可以在浏览器中访问
http://localhost:3000/api-docs来查看和测试你的API文档。
请注意,这些步骤假设你已经有一个运行中的Express应用程序。如果你还没有Express应用程序,你需要先创建一个。此外,Swagger UI的版本和安装方法可能会随着时间而变化,所以请确保查看最新的官方文档以获取最准确的信息。
以上就是关于“在Linux系统中如何集成Swagger UI”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm