johnny johnny - 1 year ago 87
Javascript Question

Easy way to store JSON under Node.js

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.

Answer Source

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");