Ruby Rack Heroku: Serving Static Files

ぃ、小莉子 提交于 2019-12-11 03:21:54

问题


I have followed the Heroku guide on deploying static files using Ruby Rack (https://devcenter.heroku.com/articles/static-sites-ruby), but I was unable to access any HTML file in \public apart from index.html. Namely, localhost:9292/test.html still maps to index.html. (All my style and js files serve correctly).

Below is my config.ru file. I know what's wrong, but not sure about a valid solution?

use Rack::Static,    :urls => ["/images", "/js", "/css"],   :root => "public"

run lambda { |env|   [
    200, 
    {
      'Content-Type'  => 'text/html', 
      'Cache-Control' => 'public, max-age=86400' 
    },
    File.open('public/index.html', File::RDONLY)   ] }

回答1:


For your config.ru file, try:

use Rack::Static,
    :urls => ["/images", "/js", "/css"],
    :root => "public"

run Rack::File.new("public")

You could also replace the last line with run Rack::Directory.new("public") which would serve up all your files, but would give a file browser like interface if someone went to the url of a directory (like the root directory)




回答2:


I have no Ruby experience and I found this boilerplate project helpful when trying to deploy a static website to Heroku:

heroku-static-site

Particularly, make sure you also have Gemfile and Gemfile.lock files besides the config.ru.

Author's project structure hosts everything in the root directory but you can easily move everything to public/ and correct the config.ru as @looby suggested.



来源:https://stackoverflow.com/questions/17189252/ruby-rack-heroku-serving-static-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!