这是我的场景:
我熟悉 Javascript,并且想创建一些简单的应用程序供个人在工作中使用,或者作为概念证明,以便在进一步使用它们并获得实际的网络服务器空间之前展示。但是我不想在我的机器上设置一个实际的数据库。如果可能的话,我更愿意读取/写入本地 JSON 文件。这是我有点卡住的地方,因为我的大部分查询都在 html 5 文件系统 API 或 Node.js(已安装在我的机器上)或现在可能存在的任何其他内容之前找到了旧信息。
有人知道使用无数据库存储或类似内容创建离线 MVP 的教程吗?或者只是关于开始时应该注意什么的一般性建议?
我一直在阅读 HTML 5 文件系统 API 和 Node.js FS 的内容,但没有示例,我发现很难知道从哪里开始。
最佳答案
On April 24th, 2014 the W3C officially matured the FileSystem API to a Working Group Note and development with this technology should be done with caution, not certain if or how long it'll stick around.
IndexedDB正在寻找一个有前途的替代方案/解决方法,也许查看 html5 rocks tutorial反而。在下面留下了我最初的回答,因为它仍然有效,但我想对文件系统 API 说一句警告。
我看到你得到了答案,但我想为文件系统 api 提供一个解决方案(你也问过)使用 cookie 你有大小限制,但要回答你问题的另一面,以下是 article I used to get started with the HTML5 File-system API
文件系统 api 中的所有其他内容都围绕着获取对文件的引用,然后创建编写器、读取器等。特定于该文件实例。当文件写入者、读取者、引用完成加载时传递/执行回调函数。
请求存储配额/获取对文件系统的引用
var onInitFs = function(fs){
fileSystem = fs;//set global reference to the filesystem
};
var errorHandler = function(e){console.log('Error', e);};
navigator.webkitPersistentStorage.requestQuota(1024*1024*1024*5, function(grantedBytes) {
window.webkitRequestFileSystem(PERSISTENT, grantedBytes, onInitFs, errorHandler);
}, errorHandler);
制作文件
fileSystem.root.getFile("filepathtoJSON", {
create: true
}, callback, errorHandler);
将JSON写入文件
fileSystem.root.getFile("filepathtoJSON", {create: true}, function(fileEntry) {
fileEntry.createWriter(function(writer) {
writer.onwriteend = function(e) {
writer.onwriteend = function(e){
callbackFunction();
}
writer.onerror = function(e3){console.log(e3);}
var blob = new Blob([JSON.stringify(targetJSONobj)]);
writer.write(blob);
};
writer.onerror = function(e3) {console.log(e3);};
writer.truncate(0);
}, errorHandler);
}, errorHandler);
从文件中读取 JSON/创建 fileReader(在回调函数中传递 JSON 对象)
fileSystem.root.getFile("filepathtoJSON", {creation:false}, function(fileEntry){
fileEntry.file(function(file){
var reader = new FileReader();
reader.onloadend = function(e) {
callback(JSON.parse(this.result));
};
reader.readAsText(file);
}, errorHandler)
}, errorHandler);
关于javascript - 没有 javascript 应用程序数据库的离线/本地存储选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21124758/