Reading, writing and storing JSON with Node on Heroku

会有一股神秘感。 提交于 2021-02-06 08:44:17

问题


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.


回答1:


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:

  • https://elements.heroku.com/search?q=redis
  • https://elements.heroku.com/search?q=mongo
  • https://devcenter.heroku.com/articles/s3


来源:https://stackoverflow.com/questions/24747422/reading-writing-and-storing-json-with-node-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!