如何使用Iron Router提供静态内容(图像,字体等) [英] How to serve static content (images, fonts etc.) using iron router
问题描述
我刚刚开始使用流星上的铁路由器.我需要在首页上显示图片.我能够使用客户端路由为家"配置路由.对于静态文件,我尝试使用google进行搜索,发现添加服务器端路由可能会有所帮助.因此,我在服务器的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
定义任何路由.难道我做错了什么?还有其他方法可以使用Iron Router提供静态文件吗?
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.
这篇关于如何使用Iron Router提供静态内容(图像,字体等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!