Matt
Matt

Reputation: 5605

Writing to file on the server in Meteor [1.0.0] (without losing the files every build)

The issue I've been having is that fs.writeFile writes to the build folder under .meteor and when rebuilt is lost.

The Assets api doesn't seem to allow for writes. All I'm looking to do is write some text to a file and load it up upon next startup.

Specific use case: Steam returns data for a shaSentryfile within their API that needs to be reused on subsequent requests, otherwise the authentication process is partially manual.

https://github.com/RJacksonm1/node-dota2 https://github.com/RJacksonm1/node-dota2/blob/master/test/index.js#L151

I've thought about using some external storage service like S3, but this is such a simple scenario--it's only the one file--but i'd like to understand how files like this should be managed in Meteor.

Upvotes: 0

Views: 1043

Answers (1)

David Weldon
David Weldon

Reputation: 64342

I recommend writing files somewhere outside of your project's directory. This avoids any potential file location and reload issues.

If the file doesn't need to survive a restart, I'd recommend using your system's temp directory like so:

var fs = Npm.require('fs');
var os = Npm.require('os');
var path = Npm.require('path');

var file = path.join(os.tmpDir(), 'foo.txt');

And then you can use readFileSync and writeFileSync with file.

Alternatively, you could specify a path using an environment variable:

> OUTPUT_DIR="$HOME/output" meteor

Then you could modify the above code to:

var file = path.join(process.env.OUTPUT_DIR, 'foo.txt');

Upvotes: 3

Related Questions