🚀 Cloudflare Workers API

完整的 RESTful API 示例 - 自动生成文档
12
API 端点
4
功能模块
8
需要认证

🔐 认证说明

需要认证的端点需要在请求头中包含:

Authorization: Bearer your-secret-api-key-123

生产环境中请使用更安全的认证方式,如 JWT Token 或 OAuth 2.0

📚 API 端点

系统

GET /api/health
健康检查
响应示例:
{ "status": "healthy", "timestamp": "ISO8601", "uptime": "online" }
cURL 示例:
curl https://myservers.codelover.us.kg/api/health

用户管理

GET /api/users 🔒 需要认证
获取所有用户列表
响应示例:
{ "success": true, "data": [], "total": 0 }
cURL 示例:
curl -H "Authorization: Bearer your-secret-api-key-123" https://myservers.codelover.us.kg/api/users
GET /api/users/:id 🔒 需要认证
获取指定用户信息
路径参数:
:id - 用户ID
响应示例:
{ "success": true, "data": {} }
cURL 示例:
curl -H "Authorization: Bearer your-secret-api-key-123" https://myservers.codelover.us.kg/api/users/:id
POST /api/users 🔒 需要认证
创建新用户
请求体:
{ "name": "用户名", "email": "email@example.com", "role": "user" }
响应示例:
{ "success": true, "message": "用户创建成功", "data": {} }
cURL 示例:
curl -H "Authorization: Bearer your-secret-api-key-123" https://myservers.codelover.us.kg/api/users -X POST -H "Content-Type: application/json" -d '{"name":"用户名","email":"email@example.com","role":"user"}'
PUT /api/users/:id 🔒 需要认证
更新用户信息
路径参数:
:id - 用户ID
请求体:
{ "name": "新名称", "email": "newemail@example.com" }
响应示例:
{ "success": true, "message": "用户更新成功", "data": {} }
cURL 示例:
curl -H "Authorization: Bearer your-secret-api-key-123" https://myservers.codelover.us.kg/api/users/:id -X PUT -H "Content-Type: application/json" -d '{"name":"新名称","email":"newemail@example.com"}'
DELETE /api/users/:id 🔒 需要认证
删除用户
路径参数:
:id - 用户ID
响应示例:
{ "success": true, "message": "用户删除成功" }
cURL 示例:
curl -H "Authorization: Bearer your-secret-api-key-123" https://myservers.codelover.us.kg/api/users/:id

文章管理

GET /api/posts
获取所有文章
查询参数:
page - 页码(默认1)
limit - 每页数量(默认10)
响应示例:
{ "success": true, "data": [], "pagination": {} }
cURL 示例:
curl https://myservers.codelover.us.kg/api/posts
GET /api/posts/:id
获取指定文章
路径参数:
:id - 文章ID
响应示例:
{ "success": true, "data": { "author": {} } }
cURL 示例:
curl https://myservers.codelover.us.kg/api/posts/:id
POST /api/posts 🔒 需要认证
创建新文章
请求体:
{ "userId": 1, "title": "文章标题", "content": "文章内容" }
响应示例:
{ "success": true, "message": "文章创建成功", "data": {} }
cURL 示例:
curl -H "Authorization: Bearer your-secret-api-key-123" https://myservers.codelover.us.kg/api/posts -X POST -H "Content-Type: application/json" -d '{"userId":1,"title":"文章标题","content":"文章内容"}'
PUT /api/posts/:id 🔒 需要认证
更新文章
路径参数:
:id - 文章ID
请求体:
{ "title": "新标题", "content": "新内容" }
响应示例:
{ "success": true, "message": "文章更新成功", "data": {} }
cURL 示例:
curl -H "Authorization: Bearer your-secret-api-key-123" https://myservers.codelover.us.kg/api/posts/:id -X PUT -H "Content-Type: application/json" -d '{"title":"新标题","content":"新内容"}'
DELETE /api/posts/:id 🔒 需要认证
删除文章
路径参数:
:id - 文章ID
响应示例:
{ "success": true, "message": "文章删除成功" }
cURL 示例:
curl -H "Authorization: Bearer your-secret-api-key-123" https://myservers.codelover.us.kg/api/posts/:id

搜索

GET /api/search
搜索文章和用户
查询参数:
q - 搜索关键词
响应示例:
{ "success": true, "query": "", "results": { "posts": [], "users": [] }, "total": 0 }
cURL 示例:
curl https://myservers.codelover.us.kg/api/search

💡 快速开始

// JavaScript 示例
// 获取所有文章
const posts = await fetch('https://myservers.codelover.us.kg/api/posts')
  .then(res => res.json());

// 创建用户(需要认证)
const newUser = await fetch('https://myservers.codelover.us.kg/api/users', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your-secret-api-key-123'
  },
  body: JSON.stringify({
    name: '王五',
    email: 'wangwu@example.com',
    role: 'user'
  })
}).then(res => res.json());