Can't view new files in meteors public directory

When a meteor app gets compiled ( meteor build app ), the public directory becomes programsweb.browserapp All files which were in the development public directory public are now accessible with http://domain.tld/file-in-public-directory.jpg

When I put a new file into the compiled public directory and try to view it in the browser, I get an error, which says that Meteor does not have a route for that url. When I do this in the development public directory it works flawless, but not in the compiled ( meteor build app ).

I need this, because I want to upload new files in that directory.

Any help?


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.


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.

链接地址: http://www.djcxy.com/p/73412.html

上一篇: 从流星的外部位置获取图像

下一篇: 无法在流星公开目录中查看新文件