Can't view new files in meteors public directory

天大地大妈咪最大 提交于 2019-12-11 13:12:18

问题


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 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?


回答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.




回答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.



来源:https://stackoverflow.com/questions/28019570/cant-view-new-files-in-meteors-public-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!