13

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?

JP.
  • 5,507
  • 15
  • 59
  • 100

6 Answers6

20

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
  • 103,170
  • 56
  • 192
  • 232
  • voting for the first solution. also if you use passenger, this just works. no routing required or redirect. – rubiii Apr 04 '10 at 18:20
  • 1
    @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 Apr 04 '10 at 18:24
  • 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 Apr 04 '10 at 19:31
  • 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. Apr 05 '10 at 09:35
6

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
Dale Campbell
  • 496
  • 5
  • 10
4
require 'sinatra'

get '/' do
  send_file File.join(settings.public_folder, 'index.html')
end

As described at Serving static files with Sinatra

Community
  • 1
  • 1
Eduardo Santana
  • 5,780
  • 3
  • 19
  • 21
3

There is a gem "sinatra-index" to solve this problem.

https://github.com/elitheeli/sinatra-index

Windix
  • 891
  • 7
  • 5
3

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
  • 6,903
  • 2
  • 38
  • 51
3

I think this is only an issue because Sinatra/Rack doesn't have the concept of a default file to serve if you just go to /. In a webserver like Apache or Nginx this is taken care of for you and usually defaults to index.html index.htm (if either exists it will get served when going to a directory with no actual filename on the end).

When everyone says this is built into Passenger, I think they really mean that it's built into Apache/Nginx. Apache/Nginx will check if the static file exists and serve it if it does, the request will never get to Rack, which is awesome for performance.

I wouldn't want to set up a redirect to the 404 page as this sort of violates the whole idea of HTTP: there should be one endpoint for everything that end point should return the true state of that endpoint. Meaning that if you go to /asdf you want the webserver to report a 404 because that's what is actually going on. If you do a redirect, now your site is saying "I used to have something here, but it moved" and then the page it redirects you to, even though it says 404 in the text on the page, is actually reported by the web server as a 200, meaning "everything is fine with this page!"

Rob Cameron
  • 9,674
  • 7
  • 39
  • 42