🌐 网站管理 API

通过 HTTP API 管理网站书签数据

网站管理 API 提供了完整的 CRUD 操作,允许您通过 HTTP 请求获取、创建、更新和删除网站书签数据。

📥 获取所有网站

GET
/api/websites

响应:200 OK,返回网站数组

[
  {
    "id": "1738368000000xyz789",
    "resourceType": "website",
    "name": "GitHub",
    "description": "全球最大的代码托管平台",
    "resourcePath": "https://github.com",
    "tags": ["开发", "代码", "开源"],
    "visitedSessions": [
      "2026-01-15T09:00:00.000Z",
      "2026-01-20T14:30:00.000Z",
      "2026-01-25T16:45:00.000Z"
    ],
    "addedDate": "2026-01-10T08:00:00.000Z",
    "rating": 5,
    "comment": "每天都要用",
    "isFavorite": true
  }
]

JavaScript

// 获取所有网站
fetch('http://localhost:3000/api/websites')
  .then(response => response.json())
  .then(websites => console.log(websites))
  .catch(error => console.error('Error:', error));

Python

import requests

# 获取所有网站
response = requests.get('http://localhost:3000/api/websites')
websites = response.json()
print(websites)

📥 获取单个网站

GET
/api/websites/:id

参数:id - 网站ID

响应:200 OK 返回网站对象,不存在则 404

{
  "id": "1738368000000xyz789",
  "resourceType": "website",
  "name": "GitHub",
  "description": "全球最大的代码托管平台",
  "resourcePath": "https://github.com",
  "tags": ["开发", "代码", "开源"],
  "visitedSessions": [
    "2026-01-15T09:00:00.000Z",
    "2026-01-20T14:30:00.000Z",
    "2026-01-25T16:45:00.000Z"
  ],
  "addedDate": "2026-01-10T08:00:00.000Z",
  "rating": 5,
  "comment": "每天都要用",
  "isFavorite": true
}

JavaScript

// 获取单个网站
const websiteId = '1738368000000xyz789';
fetch(`http://localhost:3000/api/websites/${websiteId}`)
  .then(response => response.json())
  .then(website => console.log(website))
  .catch(error => console.error('Error:', error));

Python

import requests

# 获取单个网站
website_id = '1738368000000xyz789'
response = requests.get(f'http://localhost:3000/api/websites/{website_id}')
website = response.json()
print(website)

➕ 创建网站

POST
/api/websites

请求体:

{
  "name": "GitHub",
  "description": "全球最大的代码托管平台",
  "resourcePath": "https://github.com",
  "tags": ["开发", "代码", "开源"],
  "rating": 5,
  "comment": "每天都要用",
  "isFavorite": true
}

响应:201 Created,返回创建的网站对象

注意:ID 和 addedDate 会自动生成

JavaScript

// 创建网站
fetch('http://localhost:3000/api/websites', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    name: 'GitHub',
    description: '全球最大的代码托管平台',
    resourcePath: 'https://github.com',
    tags: ['开发', '代码', '开源'],
    rating: 5
  })
})
  .then(response => response.json())
  .then(website => console.log('Created:', website))
  .catch(error => console.error('Error:', error));

Python

import requests

# 创建网站
website_data = {
    'name': 'GitHub',
    'description': '全球最大的代码托管平台',
    'resourcePath': 'https://github.com',
    'tags': ['开发', '代码', '开源'],
    'rating': 5
}

response = requests.post('http://localhost:3000/api/websites', json=website_data)
website = response.json()
print('Created:', website)

✏️ 更新网站

PUT
/api/websites/:id

参数:id - 网站ID

请求体:要更新的字段(JSON 对象,只需传要修改的字段)

{
  "description": "全球最大的代码托管和协作平台",
  "tags": ["开发", "代码", "开源", "协作"],
  "rating": 5
}

响应:200 OK,返回更新后的网站对象

注意:ID 和 addedDate 字段不允许修改

JavaScript

// 更新网站
const websiteId = '1738368000000xyz789';
fetch(`http://localhost:3000/api/websites/${websiteId}`, {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    description: '全球最大的代码托管和协作平台',
    tags: ['开发', '代码', '开源', '协作']
  })
})
  .then(response => response.json())
  .then(website => console.log('Updated:', website))
  .catch(error => console.error('Error:', error));

Python

import requests

# 更新网站
website_id = '1738368000000xyz789'
update_data = {
    'description': '全球最大的代码托管和协作平台',
    'tags': ['开发', '代码', '开源', '协作']
}

response = requests.put(f'http://localhost:3000/api/websites/{website_id}', json=update_data)
website = response.json()
print('Updated:', website)

🗑️ 删除网站

DELETE
/api/websites/:id

参数:id - 网站ID

响应:204 No Content(成功)或 404 Not Found(不存在)

注意:删除操作仅移除管理器中的网站书签

JavaScript

// 删除网站
const websiteId = '1738368000000xyz789';
fetch(`http://localhost:3000/api/websites/${websiteId}`, {
  method: 'DELETE'
})
  .then(response => {
    if (response.status === 204) {
      console.log('Website deleted successfully');
    }
  })
  .catch(error => console.error('Error:', error));

Python

import requests

# 删除网站
website_id = '1738368000000xyz789'
response = requests.delete(f'http://localhost:3000/api/websites/{website_id}')

if response.status_code == 204:
    print('Website deleted successfully')

📊 网站数据字段说明

网站对象包含以下字段:

  • id - 网站唯一标识符(自动生成,不可修改)
  • resourceType - 资源类型,固定为 "website"
  • name - 网站名称(必需)
  • description - 网站描述
  • resourcePath - 网站URL地址
  • tags - 标签数组
  • visitedSessions - 访问会话时间数组(ISO 字符串数组,记录每次访问时间)
  • addedDate - 添加日期(自动生成,不可修改)
  • rating - 评分(1-5 或 0)
  • comment - 备注
  • isFavorite - 是否收藏

⚠️ 错误处理

API 使用标准的 HTTP 状态码:

  • 200 OK - 请求成功
  • 201 Created - 资源创建成功
  • 204 No Content - 删除成功
  • 400 Bad Request - 请求参数错误(如缺少必需字段)
  • 404 Not Found - 资源不存在
  • 500 Internal Server Error - 服务器内部错误

错误响应格式:

{
  "error": "网站名称不能为空"
}