Reading, writing and storing JSON with Node on Heroku

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.

Answers


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:


Need Your Help

How to make IRouteConstraint filter route

asp.net asp.net-mvc routing asp.net-mvc-routing

I wrote a custom route constraint, but its filter just doesn't get recognized. Does anyone have an example working use of IRouteConstraint ?

Is it possible to put Google Analytics code in an external JS file?

javascript google-analytics

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");