I'm looking for a super-simple way to store one JSON array in a persistent way under Node.js. It doesn't need to have any special features. I just want to put an JSON object in there and be able to read it at the next server restart. Mongo.db or Couch.db seem like an overkill for this purpose.
我正在寻找一种超级简单的方法来以一种持久的方式在Node.js下存储一个JSON数组。它不需要任何特殊的特性。我只想把一个JSON对象放在那里,然后在下次服务器重启时读取它。蒙戈。db或沙发上。db看起来是为了这个目的而过度使用的。
6 个解决方案
#1
63
Why not write to a file?
为什么不写一个文件呢?
// Writing...
var fs = require("fs");
var myJson = {
key: "myvalue"
};
fs.writeFile( "filename.json", JSON.stringify( myJson ), "utf8", yourCallback );
// And then, to read it...
myJson = require("./filename.json");
#2
5
Try NeDB: https://github.com/louischatriot/nedb
试试NeDB:https://github.com/louischatriot/nedb
"Embedded persistent database for Node.js, written in Javascript, with no dependency (except npm modules of course). You can think of it as a SQLite for Node.js projects, which can be used with a simple require statement. The API is a subset of MongoDB's. You can use it as a persistent or an in-memory only datastore."
“用于节点的嵌入式持久数据库。js,用Javascript编写,没有依赖关系(当然除了npm模块)。您可以将其视为Node的SQLite。js项目,可以使用一个简单的require语句。API是MongoDB的一个子集。您可以将它用作一个持久的或内存中唯一的数据存储。
#3
5
I found a library named json-fs-store to serialize JavaScript object to JSON in a file and retrieve it later.
我找到了一个名为JSON -fs-store的库,用于将JavaScript对象序列化到一个文件中的JSON并在以后检索它。
When retrieving a file via the store.load
method (not described in the docs at the moment), it is parsed with JSON.parse
which is better than doing a require
like in the other answer:
当通过存储检索文件时。load方法(目前文档中没有描述),它使用JSON解析。解析哪个比做其他答案中的要求更好:
- you get proper error handling when the contents are malformed
- 当内容出现错误时,您将得到正确的错误处理
- if someone has managed to sneak JavaScript code into the file, it will lead to a parse error instead of the JS being executed.
- 如果有人设法将JavaScript代码偷偷放入文件中,将导致解析错误,而不是正在执行的JS。
#4
2
You could just save the JSON-data in a simple textfile and read it at server restart.
您可以将json数据保存在一个简单的textfile中,并在服务器重启时读取它。
require('yourarrayfile.json')
and then work with it as usual.
然后像往常一样工作。
#5
1
If you are looking for performance consider: LokiJS
如果你正在寻找性能考虑:LokiJS
https://www.npmjs.com/package/lokijs
https://www.npmjs.com/package/lokijs
http://lokijs.org/ /
#6
0
Although both LokiJS and NeDB look to have more users, I just came across TaffyDB. Although I have not tried it myself I thought I should add it here for completeness.
虽然LokiJS和NeDB看起来都有更多的用户,但我还是遇到了TaffyDB。虽然我自己还没有尝试过,但我认为我应该把它添加到这里来作为完整性。
#1
63
Why not write to a file?
为什么不写一个文件呢?
// Writing...
var fs = require("fs");
var myJson = {
key: "myvalue"
};
fs.writeFile( "filename.json", JSON.stringify( myJson ), "utf8", yourCallback );
// And then, to read it...
myJson = require("./filename.json");
#2
5
Try NeDB: https://github.com/louischatriot/nedb
试试NeDB:https://github.com/louischatriot/nedb
"Embedded persistent database for Node.js, written in Javascript, with no dependency (except npm modules of course). You can think of it as a SQLite for Node.js projects, which can be used with a simple require statement. The API is a subset of MongoDB's. You can use it as a persistent or an in-memory only datastore."
“用于节点的嵌入式持久数据库。js,用Javascript编写,没有依赖关系(当然除了npm模块)。您可以将其视为Node的SQLite。js项目,可以使用一个简单的require语句。API是MongoDB的一个子集。您可以将它用作一个持久的或内存中唯一的数据存储。
#3
5
I found a library named json-fs-store to serialize JavaScript object to JSON in a file and retrieve it later.
我找到了一个名为JSON -fs-store的库,用于将JavaScript对象序列化到一个文件中的JSON并在以后检索它。
When retrieving a file via the store.load
method (not described in the docs at the moment), it is parsed with JSON.parse
which is better than doing a require
like in the other answer:
当通过存储检索文件时。load方法(目前文档中没有描述),它使用JSON解析。解析哪个比做其他答案中的要求更好:
- you get proper error handling when the contents are malformed
- 当内容出现错误时,您将得到正确的错误处理
- if someone has managed to sneak JavaScript code into the file, it will lead to a parse error instead of the JS being executed.
- 如果有人设法将JavaScript代码偷偷放入文件中,将导致解析错误,而不是正在执行的JS。
#4
2
You could just save the JSON-data in a simple textfile and read it at server restart.
您可以将json数据保存在一个简单的textfile中,并在服务器重启时读取它。
require('yourarrayfile.json')
and then work with it as usual.
然后像往常一样工作。
#5
1
If you are looking for performance consider: LokiJS
如果你正在寻找性能考虑:LokiJS
https://www.npmjs.com/package/lokijs
https://www.npmjs.com/package/lokijs
http://lokijs.org/ /
#6
0
Although both LokiJS and NeDB look to have more users, I just came across TaffyDB. Although I have not tried it myself I thought I should add it here for completeness.
虽然LokiJS和NeDB看起来都有更多的用户,但我还是遇到了TaffyDB。虽然我自己还没有尝试过,但我认为我应该把它添加到这里来作为完整性。