在线 mock 方案

1. My JSON Server(目前最稳定)

GitHub + 免费托管,完美符合你的需求:

使用方法:

  1. 创建 GitHub 仓库(例如:my-mock-api
  2. 添加 db.json 文件到仓库
  3. 访问 API
    https://my-json-server.typicode.com/你的用户名/仓库名
    

示例:

// 你的 db.json 文件内容
{
  "users": [
    { "id": 1, "name": "张三", "email": "zhang@test.com" },
    { "id": 2, "name": "李四", "email": "li@test.com" }
  ],
  "posts": [
    { "id": 1, "title": "文章1", "authorId": 1 },
    { "id": 2, "title": "文章2", "authorId": 2 }
  ]
}

访问地址:

GET    https://my-json-server.typicode.com/username/repo/users
GET    https://my-json-server.typicode.com/username/repo/users/1
POST   https://my-json-server.typicode.com/username/repo/users
PUT    https://my-json-server.typicode.com/username/repo/users/1
DELETE https://my-json-server.typicode.com/username/repo/users/1

特点:

  • ✅ 完全免费
  • ✅ 支持真实 CRUD(数据会变,但不是永久存储)
  • ✅ 无需服务器
  • ✅ 基于 GitHub,版本可控

2. MockAPI.io

网址: https://mockapi.io/

特点:

  • 可视化创建 API
  • 支持完整的 CRUD
  • 可以设置响应格式
  • 有免费套餐(每月 10000 次请求)

创建步骤:

  1. 注册账号
  2. 点击 "New Project"
  3. 创建 Resource(如 users
  4. 手写 JSON 或使用表单添加
  5. 获得 API 端点

示例端点:

https://64d8f6e25f9bf5b879cea5c2.mockapi.io/api/v1/users

3. DummyJSON

网址: https://dummyjson.com/

虽然主要提供预定义数据,但支持修改:

// 更新数据示例
fetch('https://dummyjson.com/users/1', {
  method: 'PUT',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    name: '修改后的名字'
  })
})

4. ReqRes.in

网址: https://reqres.in/

主要用于测试,支持 CRUD:

POST   https://reqres.in/api/users
PUT    https://reqres.in/api/users/2
DELETE https://reqres.in/api/users/2

5. JSONBin.io

网址: https://jsonbin.io/

专门为 JSON 设计的在线存储:

创建步骤:

  1. 创建 bin
  2. 手写 JSON 数据
  3. 获得 API 密钥
  4. 使用 REST API 访问
// 创建 bin
fetch('https://api.jsonbin.io/v3/b', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-Master-Key': '$2a$10$...'
  },
  body: JSON.stringify({
    users: [
      { id: 1, name: "张三" }
    ]
  })
})

6. RapidAPI(多个提供商)

网址: https://rapidapi.com/hub

搜索 "mock api" 有很多服务商,部分支持自定义数据。

7. 部署到免费云平台(最灵活)

Vercel + JSON Server

  1. 创建 GitHub 仓库
  2. 添加 api/index.js
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();

server.use(middlewares);
server.use(router);

module.exports = server;
  1. 部署到 Vercel(免费)

GitHub Pages + JSON Server

需要一些配置,但完全免费。

对比推荐

服务 手写JSON 真实CRUD 免费额度 推荐指数
My JSON Server ⚠️(临时) 无限制 ⭐⭐⭐⭐⭐
MockAPI.io 10k/月 ⭐⭐⭐⭐
JSONBin.io 有限 ⭐⭐⭐⭐
ReqRes.in 无限制 ⭐⭐⭐
RapidAPI 看提供商 看提供商 各不相同 ⭐⭐

最佳实践方案

方案A:快速开始(推荐)

  1. 使用 My JSON Server
  2. 在 GitHub 创建 db.json
  3. 通过 typicode 访问
  4. 更新数据时 push 到 GitHub

方案B:长期项目

  1. 使用 MockAPI.io
  2. 创建免费账号
  3. 通过界面管理数据
  4. 使用提供的 API 端点

方案C:需要复杂逻辑

  1. 部署到 Vercel
  2. 使用 JSON Server 或自定义 Express
  3. 获得完全控制权

具体操作示例(My JSON Server)

  1. 创建 GitHub 仓库my-mock-data
  2. 创建 db.json
{
  "products": [
    {
      "id": 1,
      "name": "iPhone 14",
      "price": 6999,
      "category": "手机"
    },
    {
      "id": 2,
      "name": "MacBook Pro",
      "price": 12999,
      "category": "电脑"
    }
  ],
  "categories": [
    { "id": 1, "name": "手机" },
    { "id": 2, "name": "电脑" }
  ]
}
  1. 访问 API
# 获取所有商品
curl https://my-json-server.typicode.com/你的用户名/my-mock-data/products

# 获取单个商品
curl https://my-json-server.typicode.com/你的用户名/my-mock-data/products/1

# 创建商品(临时)
curl -X POST https://my-json-server.typicode.com/你的用户名/my-mock-data/products \
  -H "Content-Type: application/json" \
  -d '{"id":3,"name":"iPad","price":4999}'

# 分页和过滤
curl "https://my-json-server.typicode.com/你的用户名/my-mock-data/products?price_gte=5000"

注意:POST/PUT/DELETE 操作会修改数据,但这些修改不会保存到 GitHub,只是临时的内存修改。

紧急备用方案

如果以上都不行,可以用这个临时在线 JSON:

// 使用这个公开的测试端点(可能有访问限制)
fetch('https://httpbin.org/anything', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ name: '测试数据' })
})
.then(res => res.json())
.then(data => console.log(data.json)); // 会返回你发送的数据

推荐:直接使用 My JSON Server,这是最接近你需求的方案!

posted @ 2026-01-06 14:57  丁少华  阅读(21)  评论(1)    收藏  举报