Static page routing in Sinatra (Ruby)

前端 未结 6 858
醉酒成梦
醉酒成梦 2021-02-13 13:39

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

相关标签:
6条回答
  • 2021-02-13 14:11

    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
    
    0 讨论(0)
  • 2021-02-13 14:11

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

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

    0 讨论(0)
  • 2021-02-13 14:20

    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.

    0 讨论(0)
  • 2021-02-13 14:25

    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

    0 讨论(0)
  • 2021-02-13 14:27

    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!"

    0 讨论(0)
  • 2021-02-13 14:29
    require 'sinatra'
    
    get '/' do
      send_file File.join(settings.public_folder, 'index.html')
    end
    

    As described at Serving static files with Sinatra

    0 讨论(0)
提交回复
热议问题