在Linux上部署Swagger服务通常涉及以下几个步骤:
安装Swagger工具:
npm install -g swagger-jsdoc swagger-ui-express
准备Swagger配置文件:
swagger.json
或swagger.yaml
,用于描述你的API接口。swagger: '2.0'
info:
title: Sample API
description: API documentation for my sample application
version: '1.0.0'
host: api.example.com
basePath: /v1
schemes:
- https
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
创建Express应用:
const express = require('express');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
const app = 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}`);
});
运行Express应用:
node app.js
访问Swagger UI:
http://<your-server-ip>:3000/api-docs
,你应该能够看到Swagger UI界面,其中包含了你的API文档。通过以上步骤,你就可以在Linux上成功部署Swagger服务,并通过Swagger UI来查看和测试你的API接口。