如何使用铁路由器提供静态内容(图像、字体等) [英] How to serve static content (images, fonts etc.) using iron router
问题描述
我刚刚开始在流星上使用铁路由器.我需要在主页上显示图像.我能够使用客户端路由为家"配置路由.对于静态文件,我尝试使用谷歌搜索,发现添加服务器端路由可能会有所帮助.因此,我在服务器的 router.js 上添加了以下代码.
I just started working with iron router on meteor. I need to show an image on homepage. I was able to configure route for 'home' using the client side routing. For static files I tried to google and found that adding a server side route might help. So, I added the following code on server's router.js.
Router.map(function() {
this.route('files', {
path: '/files/:path(*)',
action: function() {
var path = this.params.path;
console.log('will serve static content @ '+path);
this.response.sendfile(path);
}
});
});
当我尝试访问 http://localhost:3000/files/someImage.png
时,它说没有为 /files/someImage.png
定义路由.难道我做错了什么?有没有其他方法可以使用铁路由器来提供静态文件?
When I try to access http://localhost:3000/files/someImage.png
, it says that no route is defined for /files/someImage.png
. Am I doing something wrong? Is there any other way to serve static files using iron router?
推荐答案
您可以将文件放在 public
目录下,而不是执行所有这些操作.如果添加文件:
Instead of doing all this, you can just put the files under your public
directory. If you add the file:
myApp/public/images/kitten.png
您可以从您的模板访问它,例如:
You can access it from your templates like:
<img src="/images/kitten.png">
不需要任何路由来实现这一点.
No routes are needed to make that work.
小心忽略斜线.
<img src="images/kitten.png">
上面的例子将在你的顶级路由上工作,比如/books 这很容易错过,但在/books/pages 上失败.
the above example will work from your top level routes like /books which makes it easy to miss, but fail on /books/pages.
这篇关于如何使用铁路由器提供静态内容(图像、字体等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!