Warz
Warz

Reputation: 7746

Reading files from a directory inside a meteor app

How can i read the public directory in a meteor application inside my /server path.

I tried using the native 'fs' package but i keep getting a file/directory not found error.

var fs = Npm.require('fs');
var files = fs.readdirSync('/public/soundfiles/');

Has anyone used the filesystem package to read static files inside a meteor application?

Upvotes: 12

Views: 16589

Answers (7)

Michel Floyd
Michel Floyd

Reputation: 20227

On the server you can use fs to access any part of the meteor directory tree, not just /public, for example

import fs from 'fs';
const rd = process.env.PWD;
const obj = JSON.parse(fs.readFileSync(`${rd}/private/file.json`));

would read and parse a json file located at private/file.json under your meteor app directory root.

Upvotes: 0

Perdi Estaquel
Perdi Estaquel

Reputation: 846

For Meteor 1.4, use server Assets. See the official docs on Assets http://docs.meteor.com/api/assets.html

Upvotes: 1

Cees Timmerman
Cees Timmerman

Reputation: 19644

This works for me in Meteor 1.0:

var fs = Npm.require('fs')
var xsd = fs.readFileSync(process.cwd().split('.meteor')[0] + 'server/company.xsd', 'utf8')

Upvotes: 4

Mattijs
Mattijs

Reputation: 3395

I learned that it is best to upload files in your private folder if you are not displaying them outside. In my case I need to store XML uploads and process them. At first I wrote the XML into the public folder but that would trigger a reload. Then I renamed the the upload folder to /public/.#uploads which would stop the reload of Meteor, but then again...it completely ignored that folder during build and the uploaded folder would not exist in the build (throw ENOENT error during read).

So I figured out it is best to put the files in /private/files and then reading goes as follows:

result = fs.readdirSync('assets/app/files')

Everything in the private folder will be moved to the Assets folder where during runtime there is an APP folder available (you do not see that in your build folder structure).

It helps to just simple dump result = fs.readdirSync('.') to see what folder you in and look through the structure.

***UPDATE***** Locally putting files in private folder still triggered meteor rebuild/update (perhaps not in production..) so I found another solution using the UploadServer just to define the upload directory: https://github.com/tomitrescak/meteor-uploads

Upvotes: 6

despi23
despi23

Reputation: 138

For meteor 1.0.2 public is /web.browser/app/ Checked by entering .meteor dir Total path in linux /home/user/your_app_name/.meteor/local/build/programs/web.browser/app/ And to get to root is `process.env.PWD or process.cwd(). Im not sure if its work deployed.

_meteor_bootstrap_.serverDir +'/assets/app'

This is path to private folder.

Upvotes: 2

This is no longer true. For Meteor 0.8, the folder "../client/app" is public. Thus, use fs.readdirSync('../client/app') to get files and folders in public.

Source: personal experience and https://stackoverflow.com/a/18405793

Upvotes: 2

BenjaminRH
BenjaminRH

Reputation: 12172

Access files without the "/public" part. In a running Meteor app, the public directory becomes your root, and everything that is located at /public/whatever can be accessed at /whatever.

Additionally, if you're playing around with files, you might find these useful:

Upvotes: 2

Related Questions