views:

292

answers:

3

You can serve static files with Sinatra by placing them in public/ (by default) -- I have an index.html in there at the moment, but how can I make the root point to that file without having to parse it as a template?

To be clear, I can access /index.html successfully, and I'd like to route / to be the same static file, but without redirecting. Any idea how to do this?

+1  A: 

Probably a better answer will eventually come, until then this is my shot at it.

If this is not what you want:

get '/' do
  redirect '/index.html'
end

You might do something like this:

get '/' do
  File.new('public/index.html').readlines
end

I'd go with the first one though, Not sure why you want to avoid that redirect

Pablo Fernandez
voting for the first solution. also if you use passenger, this just works. no routing required or redirect.
rubiii
@rubii I think it would be worth putting the passenger way in it's own answer. That seems like a more correct answer than mine
Pablo Fernandez
i think your answer is perfectly fine. i would bet that JP is not using passenger. but i'll post an answer for everyone else.
rubiii
I don't really need to avoid the redirect, I'm just a fan of 'clean' URLs :) (`/` over `/index.html` and `/help` over `/help.html`) -- no worry though, redirect looks like the way to go, thanks!
JP
+1  A: 

using passenger this seems to work right out of the box. having an index.html file in the public directory and no routing brings up the index.html when accessing the root url.

rubiii
A: 

Just set enable :static inside of your app class. Like so:

class App < Sinatra::Base
  # Set the app root; this is where the 'public' and 'views'
  # directories are found (by default).
  set :root, File.expand_path("#{File.dirname(__FILE__)}/../app")

  # Allow the app to serve static files from the 'public' directory in :root
  enable :static
end
Oshuma