Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Sinatra静态页面_Ruby_Sinatra - Fatal编程技术网

Ruby Sinatra静态页面

Ruby Sinatra静态页面,ruby,sinatra,Ruby,Sinatra,在我的公用文件夹中,我有一个文件index.html。我希望能够只使用url localhost:4567来显示页面(而不显示localhost:4567/index.html) 这是我当前的ruby脚本: require 'sinatra' set :public_folder, 'public' get '/' do redirect '/index.html' end 我已尝试删除重定向语句,但url仍会显示index.html。重定向背后的想法就是强制浏览器向您要重定向到的位置

在我的公用文件夹中,我有一个文件index.html。我希望能够只使用url localhost:4567来显示页面(而不显示localhost:4567/index.html)

这是我当前的ruby脚本:

require 'sinatra'

set :public_folder, 'public'

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

我已尝试删除重定向语句,但url仍会显示index.html。

重定向背后的想法就是强制浏览器向您要重定向到的位置发出新请求。 由于您不想更改url,因此有两种可能的解决方案:

  • 重写而不是重定向。Sinatra本身不提供此类功能,但您可以轻松使用机架中间件:

    require 'rack/rewrite'
    
    use Rack::Rewrite do
      rewrite '/', '/index.html'
    end
    
  • 当请求根路径时,提供index.html内容:

    get '/' do
      File.read("#{APP_ROOT}/public/index.html")
    end
    
  • 您可以在此处使用:


    您需要提供工作目录中文件的完整路径(即不仅仅是
    public
    下的路径),并且这只适用于根url,通常不会为目录提供
    index.html
    页面。如果您想这样做,您可能需要在Sinatra前面设置一个单独的web服务器,并对其进行适当的配置。

    @matt的文件服务方式更好。非常有效!!非常感谢
    get "/" do
      send_file 'public/index.html'
    end