node.js createWriteStream doesn't create new f

2019-07-21 16:37发布

问题:

I have following code that works fine on my localhost running node.js 0.12.0. The code creates a new file, and copy data from readable, but it doesn't create new file on Heroku.

var output = fs.createWriteStream('public/images/test/testfile.png');
readable.pipe(output);

I thought it has something to do with the permission, but whenever I change the permission on the folder using heroku run bash and then chmod -R 777 images/ Heroku resets it back to its original permission which is drwx------.

So may be the problem is something else?

Please note that it fails silently, no exception, nothing in the log.

回答1:

In Heroku a dyno's local file storage is not persistent (besides the git repo files obviously), so if you write a local file and the dyno restarts the file will be gone, and if you start another dyno it won't be able to "see" the file.

heroku run bash starts a new "one-off" dyno (can read about it here: https://devcenter.heroku.com/articles/one-off-dynos), so the file will not be accessible that way.

If you want your data to persist, better use some database or persistent storage addon.