0

I made a simple Node.js server and deployed it to Heroku that serves a dead simple HTML form that returns submitted value to

fs.writeFile('./users.json',JSON.stringify(dataArr,null,4),(err)=>{
    if(err){console.log(err);}
    console.log('file saved');
  });

In Heroku's logs I can see "new file written" but I can't find a way to retrieve the saved information from the users.json file. When I try to do a pull request from Heroku, it says that everything is already up to date, even though in Heroku logs I can see "new file written".

How can I see the change in my users.json file?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Glitch Fish
  • 41
  • 1
  • 13

1 Answers1

1

You should be able to open it from your JavaScript code like any other file, but there's a huge catch: Heroku's filesystem is ephemeral. Any changes you make to it will be lost when your dyno restarts, which happens frequently.

If you need to store data for any meaningful amount of time you'll have to put it elsewhere. Heroku recommends using something like Amazon S3 for file storage, or you could use a client-server database like PostgreSQL.

Based on its filename I'd guess a database is a better option here, though of course without knowing what users.json actually contains and how it's used I can't say for sure.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • The problem is i dont know how to access new instance of users.json(file that stores newly created username/pass) on heroku. when i try to 'git pull heroku master' it doesn't update new users that have added new username/password via fs.writeFile,but instead it trows me a message sayng, 'everything is up to date' even tho users.json file has changed on heroku.(of course before dyno reset). – Glitch Fish Apr 20 '18 at 14:23
  • @GlitchFish, committing changes to files on Heroku and pulling them to your local machine isn't a good strategy here (but of course you'd need to `add` and `commit` the files before you can `pull` new commits). For starters, data shouldn't be tracked in your repository. Ignoring that, you can't really predict when dyno restarts will happen so there's a significant opportunity to lose data. I _strongly_ urge you to save your data some other way, e.g. into a PostgreSQL database (probably better) or a remote object store like Amazon S3 as I have suggested. – ChrisGPT was on strike Apr 20 '18 at 14:27
  • @GlitchFish, as an aside, I hope you are also pushing your work to a real Git host: "[Heroku provides the git service primarily for deployment, and the ability to clone from it is offered as a convenience. We strongly recommend you store your code in another git repository such as GitHub and treat that as canonical.](https://devcenter.heroku.com/articles/git-clone-heroku-app)" – ChrisGPT was on strike Apr 20 '18 at 14:29