在Node.js下存储JSON的简便方法 [英] Easy way to store JSON under Node.js
本文介绍了在Node.js下存储JSON的简便方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种超级简单的方法来在 Node.js 下以持久的方式存储一个 JSON 数组。它不需要任何特殊功能。我只想在其中放置一个JSON对象,并能够在下次服务器重启时读取它。
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 a JSON object in there and be able to read it at the next server restart.
(像 MongoDB 和 CouchDB 对于这个目的来说似乎都有点过分。)
(Solutions like MongoDB and CouchDB both seem like overkill for this purpose.)
推荐答案
为什么不写入文件?
// 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");
这篇关于在Node.js下存储JSON的简便方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文