
























GitHub + 免费托管,完美符合你的需求:
my-mock-api)db.json 文件到仓库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
特点:
users)https://64d8f6e25f9bf5b879cea5c2.mockapi.io/api/v1/users
虽然主要提供预定义数据,但支持修改:
// 更新数据示例
fetch('https://dummyjson.com/users/1', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: '修改后的名字'
})
})
主要用于测试,支持 CRUD:
POST https://reqres.in/api/users
PUT https://reqres.in/api/users/2
DELETE https://reqres.in/api/users/2
专门为 JSON 设计的在线存储:
// 创建 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: "张三" }
]
})
})
搜索 "mock api" 有很多服务商,部分支持自定义数据。
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;
需要一些配置,但完全免费。
| 服务 | 手写JSON | 真实CRUD | 免费额度 | 推荐指数 |
|---|---|---|---|---|
| My JSON Server | ✅ | ⚠️(临时) | 无限制 | ⭐⭐⭐⭐⭐ |
| MockAPI.io | ✅ | ✅ | 10k/月 | ⭐⭐⭐⭐ |
| JSONBin.io | ✅ | ✅ | 有限 | ⭐⭐⭐⭐ |
| ReqRes.in | ❌ | ✅ | 无限制 | ⭐⭐⭐ |
| RapidAPI | 看提供商 | 看提供商 | 各不相同 | ⭐⭐ |
db.jsontypicode 访问my-mock-datadb.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": "电脑" }
]
}
# 获取所有商品
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,这是最接近你需求的方案!
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。