admin管理员组文章数量:1405170
Here is my scenario:
I am familiar with Javascript and would like to create some simple apps to use at work either personally or as proofs of concepts to present before taking them further and getting actual web server space. I do not however want to setup an actual database on my machine. I would prefer, if possible, to read/write to a local JSON file. This is where I am a bit stuck as most of my queries have turned up old information prior to the html 5 FileSystem API or Node.js (already installed on my machine) or whatever else there may be now.
Does anyone know of a tutorial for creating an offline MVP with database-less storage or something similar? Or just general advice on what to look at to get started?
I have been reading the HTML 5 Filesystem API and Node.js FS stuff but without an example I am finding it quite difficult to know where to begin.
Here is my scenario:
I am familiar with Javascript and would like to create some simple apps to use at work either personally or as proofs of concepts to present before taking them further and getting actual web server space. I do not however want to setup an actual database on my machine. I would prefer, if possible, to read/write to a local JSON file. This is where I am a bit stuck as most of my queries have turned up old information prior to the html 5 FileSystem API or Node.js (already installed on my machine) or whatever else there may be now.
Does anyone know of a tutorial for creating an offline MVP with database-less storage or something similar? Or just general advice on what to look at to get started?
I have been reading the HTML 5 Filesystem API and Node.js FS stuff but without an example I am finding it quite difficult to know where to begin.
Share asked Jan 14, 2014 at 21:51 tehaarontehaaron 2,25010 gold badges31 silver badges54 bronze badges 7- You prefer a storage on client-side or server-side ? – kevpoccs Commented Jan 14, 2014 at 21:53
- @kevpoccs client side as there will be no server – tehaaron Commented Jan 14, 2014 at 21:58
-
If you're looking for client-side storage, then node's
fs
module probbly isn't what you're looking for. That's a server-side thing. – juanpaco Commented Jan 14, 2014 at 22:01 - @teharron, Have you ever take a look at the Web SQL Database ? – kevpoccs Commented Jan 14, 2014 at 22:02
- 1 @juanpaco thanks for the heads-up, wasn't sure if it would be of use or not since I wont be running node on a server. – tehaaron Commented Jan 14, 2014 at 22:12
2 Answers
Reset to default 4On 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.
The IndexedDB is looking to be a promising alternative/work-around, perhaps check out the html5 rocks tutorial instead. Left my initial answer below because it is still valid, but wanted to leave a word of caution about the File-System API.
I see you got an answer but I wanted to give a solution to the file-system api (which you also asked about) With cookies you have a size limitation, but to answer the other side of your question the following is the article I used to get started with the HTML5 File-system API
Everything else within the file-system api revolves around getting the reference to a file and then creating writers, readers, ect. specific for that file instance. Callback functions are passed/to be executed when file writers, readers, references have pleted loading.
Requesting storage Quota/get a reference to the FileSystem
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);
Making a file
fileSystem.root.getFile("filepathtoJSON", {
create: true
}, callback, errorHandler);
Writing JSON to file
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);
Reading JSON from a file/creating a fileReader(pass JSON object in a callback function)
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);
You could write it yourself by turning the object to a string and storing as a cookie or using local storage:
var data = { 'example': 324324 };
document.cookie = JSON.stringify(data);
var loaded = document.cookie.split(';');
console.log(JSON.parse(loaded [0]));
Or alternatively use a library which will do it for you!
http://brian.io/lawnchair/
本文标签: nodejsOptions for offlinelocal storage without a database for javascript appStack Overflow
版权声明:本文标题:node.js - Options for offlinelocal storage without a database for javascript app - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744884913a2630428.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论