Read file from the public folder


#1

I’m using fs.readFileSync(“xxx”) and meteor 1.0.1

I’ve looked here:

And none of the suggestions are working for me. Any other suggestions?

Thanks!


#2

The public directory appears to be located at .meteor/local/build/programs/web.browser/app/ but the easiest way I would think to load files from the public directory is to create a symlink inside the private directory so that Assets.getText() can be used. It’s normally used to read files from the private directory but I see no reason why it couldn’t be used for both.

$ mkdir private
$ cd private
# Create symlink to public directory inside private directory
$ ln -s ../public public

Then on the server side of your application:

var fileContents = Assets.getText('public/myfile.txt');

#3

Thanks for the info on public, and I notice that the files in my public folder do get copied to that directory after my project has been built. With that being the case, why can’t I just do:

var fileContents = Assets.getText('/myfile.txt');

or

 var stream = fs.readFileSync('/myfile.txt');

neither of these attemps work even though the myfile.txt file is in the …/app directory. I get an ‘Exited with code: 8’ error.


#4

To use fs.readFileSync, you probably need to get the relative directory correct. Trying using process.cwd() and path.join() to get to the full path to myfile.txt.


#5

Interestingly, process.cwd() returns:

C:\...\ground-control\.meteor\local\build\programs\server

not

C:\...\ground-control\.meteor\local\build\programs\web.browser\app\

but either way, I can’t get the path.join call to work. It always causes an exit with code 8 when written as below:

var workingDir = process.cwd();
var fullPath = path.join(workingDir, 'NFS2_small.ICD');

I also tried the fs.readFileSync as shown below, but neither of those worked either, both causing the app to crash with exit code 8:

    var workingDir = process.cwd();
    var stream = fs.readFileSync(workingDir + '\..\web.browser\app\NFS2_small.ICD');

   var workingDir = process.cwd();
    var stream = fs.readFileSync(workingDir + '\NFS2_small.ICD');

#6

be careful with process.cwd as it changes based on the deployment env.

I need to access a directory path for loading a list of files

// files in /private get built to:
//      .meteorlocal/build/programs/server/assets/app/
// base path resolves to:
//      .meteor/local/build/programs/server

so you need to manually add “/assets/app” to your paths.

until meteor change this at some point.

just getting to the content of a file isn’t helpful if you have a directory of changing content…