Swagger(現在稱為OpenAPI)是一個用于設計、構建、記錄和使用RESTful Web服務的框架。要在Linux下實現Swagger的實時監控,你可以使用以下步驟:
wget https://github.com/swagger-api/swagger-ui/archive/master.zip
unzip master.zip
cd swagger-ui-master
安裝Node.js和npm:Swagger UI需要Node.js和npm(Node.js包管理器)來運行。如果你還沒有安裝它們,請訪問Node.js官方網站(https://nodejs.org/)下載并安裝適用于Linux的安裝包。
安裝Swagger UI Express:在你的項目目錄中,使用npm安裝Swagger UI Express,這是一個將Swagger UI集成到你的Node.js應用程序的庫:
npm install swagger-ui-express
app.js
的文件,并添加以下內容:const express = require('express');
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
const app = express();
const port = process.env.PORT || 3000;
// 讀取Swagger規范文件
const swaggerDocument = YAML.load('./swagger.yaml');
// 將Swagger規范文件添加到Express應用程序中
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
// 啟動Express服務器
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
swagger.yaml
的文件,并添加你的API規范。例如:swagger: '2.0'
info:
title: Sample API
description: A sample API to demonstrate Swagger UI real-time monitoring
version: '1.0.0'
host: localhost:3000
basePath: /api
schemes:
- http
paths:
/users:
get:
summary: List all users
responses:
200:
description: An array of users
node app.js
http://localhost:3000/api-docs
,查看你的API規范,并進行實時監控。請注意,這只是一個簡單的示例,你可以根據自己的需求對其進行擴展。例如,你可以添加更多的API端點、參數、響應等。你還可以考慮使用Docker來部署你的應用程序,以便在不同的環境中輕松地運行和擴展。