无法查看流星公共目录中的新文件 [英] Can't view new files in meteors public directory
问题描述
当一个meteor 应用程序被编译(meteor build app
)时,公共目录变成\programs\web.browser\app
开发公共目录中的所有文件\public
现在可以通过 http://domain.tld/file-in-public-directory.jpg
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
当我将一个新文件放入编译后的公共目录并尝试在浏览器中查看它时,我收到一个错误,说 Meteor 没有该 url 的路由.当我在开发公共目录中执行此操作时,它可以完美运行,但在已编译的 (meteor build app
) 中却没有.
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.
有什么帮助吗?
推荐答案
所以,您必须稍微调整一下,但是有一种方法可以访问几乎任何您想要的文件夹,尤其是对于静态文件,通过使用低级 connectHandlers
对象.
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.
这是一个示例,其中您有一个名为 .images 的文件夹(从 Meteor 自动刷新中隐藏),每当向 http://[yoursiteURL]/images/...
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();
}
});
您正在使用正则表达式来确定传入请求是否正在尝试访问/images/.如果是,我们将使用 res.write()
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()
注意两点:
1- 你不必使用任何特殊的东西(没有包等)来使用 Npm.require('fs')
因为它是内置的并且可用.
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- 使用 fs.readFileSync
有点麻烦,而且会阻塞.您需要针对高性能的生产应用进行调整.
2- using fs.readFileSync
is a bit of a hack, and is blocking. You'll want to tweak that for a performant, production app.
希望这有帮助!可以在此处找到有关 connectHandlers
的更多信息.
Hope this helps! A bit more information on connectHandlers
can be found here.
这篇关于无法查看流星公共目录中的新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!