使用Rack提供静态文件

时间:2015-03-13 12:12:47

标签: ruby rack cuba

我有这段代码:

require "cuba"
require "mote"
require "mote/render"

Cuba.plugin(Mote::Render)

Cuba.use Rack::Static,
 # urls: %w[/index],
  root: File.expand_path("./public", __dir__)

Cuba.define do
  on(root) do
      render("index", title: "Welcome")
  end

end

我试图将公共文件夹中的文件服务器(与我正在运行的此文件位于同一目录中)命名为" index.html",但我和#39;我的网站上收到错误,说无法找到。

No such file or directory @ rb_sysopen - /root/views/index.html.mote

有任何帮助吗?提前谢谢!

2 个答案:

答案 0 :(得分:2)

古巴尝试渲染模板,因此您可以将文件重命名为.mote,它应该渲染正常,或者使用类似的东西:

res.headers["Content-Type"] = "text/html; charset=utf-8"
res.write(IO.read('/path/to/your/file.html'))

Source非常清楚渲染函数的工作原理。

答案 1 :(得分:1)

您还可以设置自己的自定义路由,以便在不使用Rack :: Static

的情况下提供css / js
on 'css', extension('css') do
    res['Content-Type'] = 'text/css'
    res.write File.read(req.path)
end
相关问题