我刚开始在 meteor 上使用铁路由器。我需要在主页上显示图像。我能够使用客户端路由为“家”配置路由。对于静态文件,我尝试使用谷歌搜索,发现添加服务器端路由可能会有所帮助。所以,我在服务器的 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
定义路由.难道我做错了什么?还有其他方法可以使用铁路由器提供静态文件吗?
最佳答案
您可以将文件放在您的 public
下,而不是执行所有这些操作。目录。如果添加文件:
myApp/public/images/kitten.png
您可以从模板访问它,例如:
<img src="/images/kitten.png">
不需要任何路由来完成这项工作。
小心忽略铅斜线。
<img src="images/kitten.png">
上面的例子将在你的顶级路由上工作,比如/books 这很容易错过,但在/books/pages 上失败。
关于meteor - 如何使用铁路由器提供静态内容(图像、字体等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21341291/