How to serve static files via Rack?

后端 未结 4 2184
Happy的楠姐
Happy的楠姐 2020-12-04 11:10

I am currently developing a Rack-based application and want to redirect all file requests(e.g. filename.filetype) to a specified folder.

Rack::Static only supports f

4条回答
  •  臣服心动
    2020-12-04 11:31

    To redirect every request to a particular path, use Rack::File (for some reason this class is absent in recent documentation, but it is still part of the latest Rack):

    run Rack::File.new("/my/path")
    

    To redirect every request, and add an HTML index of all files in the target dir, use Rack::Directory:

    run Rack::Directory.new("/my/path")
    

    To combine several directories or serve only a some requests from the target dir:

    map "/url/prefix" do
      run Rack::File.new("/my/path")
    end
    
    # More calls to map if necessary...
    
    # All other requests.
    run MyApp.new
    

提交回复
热议问题