在Heroku上使用Node读取,写入和存储JSON [英] Reading, writing and storing JSON with Node on Heroku

查看:71
本文介绍了在Heroku上使用Node读取,写入和存储JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于在Heroku上运行的Node.js构建应用程序.

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

该应用程序使用一个JSON文件,此文件目前正与应用程序的其余部分一起推送,我们正在像这样读取和写入该文件:

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

这对于原型现在已经有效,但是我知道我们需要使用数据存储,否则当Dyno睡觉或我推送更改时,更改将不会持久.

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.

我已经读到,用Mongolabs设置数据库可能是一个不错的选择,但是我想知道是否还有其他选择,因为这似乎比必要的要复杂.对我来说这是新领域,所以如果要走Mongo,那么指针也将不胜感激.

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.

干杯.

推荐答案

披露:我是Heroku的node.js平台所有者.

您需要将状态带出应用程序.要在节点应用程序中替换JSON文件,您应该查看mongo,s3和redis:

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 :功能丰富且速度相当快
  • s3 :最能映射到文件系统"存储的抽象(请谨慎使用权限)
  • redis :简单而快速
  • mongo: feature-rich and reasonably fast
  • s3: abstraction that maps best to 'filesystem' storage (take care with permissions)
  • redis: simple and fast

就个人而言,我更喜欢将Redis用于简单的用例(听起来您可能有资格).您可以转储JSON并将其解析出来.另外,最受欢迎的节点redis客户端为简单的哈希提供友好的界面:

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:

Redis, mongo, s3:

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

这篇关于在Heroku上使用Node读取,写入和存储JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆