Static File serving using Rack

695 Views Asked by At

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!

2

There are 2 best solutions below

1
Eugene Petrov On BEST ANSWER

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.

0
Matthew Billie On

You can also set up your own custom routes for serving css/js without using Rack::Static

on 'css', extension('css') do
    res['Content-Type'] = 'text/css'
    res.write File.read(req.path)
end