Ruby Sinatra静态页面
在我的公用文件夹中,我有一个文件index.html。我希望能够只使用url localhost:4567来显示页面(而不显示localhost:4567/index.html) 这是我当前的ruby脚本: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。重定向背后的想法就是强制浏览器向您要重定向到的位置
require 'sinatra'
set :public_folder, 'public'
get '/' do
redirect '/index.html'
end
我已尝试删除重定向语句,但url仍会显示index.html。重定向背后的想法就是强制浏览器向您要重定向到的位置发出新请求。 由于您不想更改url,因此有两种可能的解决方案:
require 'rack/rewrite'
use Rack::Rewrite do
rewrite '/', '/index.html'
end
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