专为静态站点设计的数据存储 API,无需复杂后端开发,几行代码即可让您的静态站点具备数据存储能力
简单易用的 RESTful API,支持创建、读取、更新 JSON 数据
3 分钟上手 FastData API
<!-- 引入 CryptoJS 用于 token 生成 -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
class FastDataClient {
constructor(ownerKey) {
this.ownerKey = ownerKey;
this.token = this.generateToken(ownerKey);
this.baseUrl = 'https://fastdata.qiangtu.com';
}
generateToken(ownerKey) {
const input = `fastdata_salt_${ownerKey}`;
const hash = CryptoJS.SHA256(input).toString();
return hash.substring(0, 16).toLowerCase();
}
// ... 其他方法
}
// 创建客户端实例
const client = new FastDataClient('my_website');
// 创建数据
const guid = await client.create({
title: '我的文章',
content: '这是文章内容',
author: '张三'
});
// 读取数据
const data = await client.get(guid);
console.log(data);
// 更新数据
data.title = '更新后的标题';
await client.save(guid, data);
// 获取新的 GUID
const newGuid = await client.getGuid();
常见场景的完整实现
创建一个简单的博客系统,支持文章发布和管理
class BlogManager {
constructor() {
this.client = new FastDataClient('my_blog');
this.postsListGuid = 'blog_posts_list';
}
async createPost(title, content, author) {
const post = {
title, content, author,
createdAt: new Date().toISOString(),
id: await this.client.getGuid()
};
await this.client.save(post.id, post);
const posts = await this.getPostsList();
posts.push(post);
await this.client.save(this.postsListGuid, posts);
return post;
}
async getPostsList() {
try {
return await this.client.get(this.postsListGuid) || [];
} catch {
return [];
}
}
}
为文章添加评论功能,支持多级评论
class CommentManager {
constructor(postId) {
this.client = new FastDataClient('my_blog');
this.postId = postId;
this.commentsGuid = `comments_${postId}`;
}
async addComment(name, content) {
const comment = {
id: await this.client.getGuid(),
name, content,
createdAt: new Date().toISOString()
};
const comments = await this.getComments();
comments.push(comment);
await this.client.save(this.commentsGuid, comments);
return comment;
}
async getComments() {
try {
return await this.client.get(this.commentsGuid) || [];
} catch {
return [];
}
}
}
直接在浏览器中测试 API 功能
{{ testResult || '暂无数据' }}