Javascript 在 Node.js 下存储 JSON 的简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14939289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easy way to store JSON under Node.js
提问by johnny
I'm looking for a super-simple way to store one JSONarray in a persistent way under Node.js. It doesn't need to have any special features. I just want to put a JSON object in there and be able to read it at the next server restart.
我正在寻找一种超级简单的方法来在Node.js下以持久的方式存储一个JSON数组。它不需要具有任何特殊功能。我只想在那里放一个 JSON 对象,并能够在下次服务器重新启动时读取它。
(Solutions like MongoDBand CouchDBboth seem like overkill for this purpose.)
(像MongoDB和CouchDB这样的解决方案对于这个目的来说似乎有点矫枉过正。)
回答by gustavohenke
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");
回答by Ray Hulha
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."
“Node.js 的嵌入式持久数据库,用 Javascript 编写,没有依赖项(当然 npm 模块除外)。你可以把它看作是 Node.js 项目的 SQLite,它可以与一个简单的 require 语句一起使用。API是 MongoDB 的一个子集。您可以将其用作持久或仅内存中的数据存储。”
回答by chiborg
I found a library named json-fs-storeto serialize JavaScript object to JSON in a file and retrieve it later.
我找到了一个名为json-fs-store 的库,用于将 JavaScript 对象序列化为文件中的 JSON 并稍后检索它。
When retrieving a file via the store.loadmethod (not described in the docs at the moment), it is parsed with JSON.parsewhich is better than doing a requirelike in the other answer:
通过该store.load方法检索文件时(目前未在文档中描述),它被解析JSON.parse为比require在另一个答案中做类似的更好:
- 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。
回答by swapnilagarwal
If you are looking for performance consider: LokiJS
如果您正在寻找性能考虑:LokiJS
https://www.npmjs.com/package/lokijs
回答by Simon Rigét
If you need simple data storage, with no external processes, very high and stable performance, you might like Rocket-store
如果你需要简单的数据存储,没有外部进程,非常高且稳定的性能,你可能会喜欢Rocket-store
It's very simple to use; Include the module and the you can start inserting records right away.
使用起来非常简单;包括该模块,您可以立即开始插入记录。
It has three major methods: put, get, and delete(and some special methods and options settings)
它有三个主要方法:put、get和delete(以及一些特殊的方法和选项设置)
Example:
例子:
// collection/table = "cars", key = "Mercedes"
result = await rs.post("cars", "Mercedes", {owner: "Lisa Simpson", reg: "N3RD"});
// Get all records from "cars" collection
result = await rs.get("cars","*");
// Delete records in collection "cars" where keys match "*cede*"
result = await rs.delete("cars","*cede*");
Rocket-storerelies on the underlying filesystem cache, to optimize for speed, and can operate with millions of records.
Rocket-store依赖底层文件系统缓存来优化速度,并且可以处理数百万条记录。
Its easily 100 times faster than a relational database, on inserts. Typically 70.000 inserts/sec. (PC)
在插入方面,它比关系数据库快 100 倍。通常为 70.000 次插入/秒。(个人电脑)
You get simple sequences, auto increment and other minor conveniences, but that about it. Nothing fancy.
您可以获得简单的序列、自动增量和其他次要的便利,但仅此而已。没有什么花哨。
I like it a lot, when ever I need a simple storage solutions, but I'm also very biased, as the author ;)
当我需要一个简单的存储解决方案时,我非常喜欢它,但作为作者,我也非常有偏见;)
回答by Soroush Hakami
You could just save the JSON-data in a simple textfile and read it at server restart.
您可以将 JSON 数据保存在一个简单的文本文件中,并在服务器重启时读取它。
const data = require("yourarrayfile.json");
and then work with it as usual.
然后像往常一样使用它。

