Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading files from a directory inside a meteor app

Tags:

node.js

meteor

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?

like image 621
Warz Avatar asked May 26 '13 19:05

Warz


3 Answers

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

like image 74
Mattijs Avatar answered Oct 23 '22 19:10

Mattijs


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')
like image 21
Cees Timmerman Avatar answered Oct 23 '22 18:10

Cees Timmerman


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:

  • FileSaver.js
  • CollectionFS
like image 30
BenjaminRH Avatar answered Oct 23 '22 18:10

BenjaminRH