Reading, writing and storing JSON with Node on Heroku

Jack Wild picture Jack Wild · Jul 15, 2014 · Viewed 11.1k times · Source

I am building an App based on Node.js running on Heroku.

The app uses a JSON file which at the moment is being pushed with the rest of the app, and we are reading and writing to it like so:

var channelsList = require("./JSON/channels.json");

...

fs.writeFile("JSON/channels.json", JSON.stringify(channelsList), onCleaned);

This has worked for now for the prototype, but I know that we need to use a data store or the changes won't persist when Dyno's sleep or I push changes.

I have read that setting up a DB with Mongolabs could be a good option, but I was wondering if there are any other options, as this seems maybe more complicated than necessary. This is new territory for me, so if Mongo is the way to go, pointers would also be appreciated.

We also want to write new files as backups for each day of the week.

Cheers.

Answer

hunterloftis picture hunterloftis · Jul 16, 2014

Disclosure: I am the node.js platform owner at Heroku.

You will need to bring the state out of your application. For replacing JSON files in a node app, you should look at mongo, s3, and redis:

  • mongo: feature-rich and reasonably fast
  • s3: abstraction that maps best to 'filesystem' storage (take care with permissions)
  • redis: simple and fast

Personally, I prefer redis for simple use cases (it sounds like yours might qualify). You can just dump JSON in and parse it out. Alternatively, the most popular redis client for node provides a friendly interface for simple hashes:

https://github.com/mranney/node_redis#friendlier-hash-commands

Redis, mongo, s3: