Skip to content Skip to sidebar Skip to footer

Can't View New Files In Meteors Public Directory

When a meteor app gets compiled (meteor build app), the public directory becomes \programs\web.browser\app All files which were in the development public directory \public are now

Solution 1:

So, you'll have to tweak it a little bit, but there's a way you can access pretty much any folder you want, especially for static files, by using the low level connectHandlers object.

Here is an example where you have a folder named .images (hidden from the Meteor auto-refresh), which serves images whenever a request is made to http://[yoursiteURL]/images/...

var fs = Npm.require('fs');
WebApp.connectHandlers.use(function(req, res, next) {
  var re = /^\/images\/(.*)$/.exec(req.url);
  if (re !== null) {   
    var filePath = process.env.PWD 
    + '/.images/' 
    + re[1];
    var data = fs.readFileSync(filePath, data);
    res.writeHead(200, {
      'Content-Type': 'image'
    });
    res.write(data);
    res.end();
  } else {  
    next();
  }
});

You're using a regular expression to find out if the incoming request is trying to access /images/. If it is, we're going to send the image with the appropriate headers, using res.write()

Two things of note:

1- you don't have to use anything special (no packages, etc) to use the Npm.require('fs') because it's built in and usable.

2- using fs.readFileSync is a bit of a hack, and is blocking. You'll want to tweak that for a performant, production app.

Hope this helps! A bit more information on connectHandlers can be found here.

Solution 2:

Nice to see folks out there trying meteor. It's great stuff - but at the same time, it does seem complicated. What really helped me so much is using this app: metoer-kitchen. I now use it alongside when working on my projects.

Post a Comment for "Can't View New Files In Meteors Public Directory"