Search code examples
rubyrackcuba

Static File serving using Rack


I have this block of code:

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

and I'm trying to server the file in the public folder(which is in the same directory as the this file I'm running) named "index.html", but I'm getting an error on my website saying it cannot be found.

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

Any help? Thanks in advance!


Solution

  • Cuba tries to render a template, so you can rename your file to .mote and it should render ok, or use something like this:

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

    Source is pretty clear on how the render function works.