Ruby 使用Sinatra服务静态文件
我有一个网页的网站只使用HTML,CSS和JavaScript。我想将应用程序部署到Heroku,但找不到方法。我现在正试图使应用程序与Sinatra一起工作Ruby 使用Sinatra服务静态文件,ruby,sinatra,Ruby,Sinatra,我有一个网页的网站只使用HTML,CSS和JavaScript。我想将应用程序部署到Heroku,但找不到方法。我现在正试图使应用程序与Sinatra一起工作 . |-- application.css |-- application.js |-- index.html |-- jquery.js `-- myapp.rb 下面是myapp.rb的内容 require 'rubygems' require 'sinatra' get "/" do # What should I writ
.
|-- application.css
|-- application.js
|-- index.html
|-- jquery.js
`-- myapp.rb
下面是myapp.rb
的内容
require 'rubygems'
require 'sinatra'
get "/" do
# What should I write here to point to the `index.html`
end
在没有任何额外配置的情况下,Sinatra将为
公共
中的资产提供服务。对于空路由,您需要呈现索引文档
require 'rubygems'
require 'sinatra'
get '/' do
File.read(File.join('public', 'index.html'))
end
路由应返回一个
字符串
,该字符串将成为HTTP响应主体<代码>文件。读取打开一个文件,读取该文件,关闭该文件并返回一个字符串
这个解决方案怎么样
get "/subdirectory/:file" do
file = params[:file] + "index.html"
if File.exists?(params[:file])
return File.open("subdirectory/" + file)
else
return "error"
end
end
因此,如果您现在导航到(例如)/子目录/test/它将加载子目录/test/index.htmlSinatra应允许您从公共目录提供静态文件: 静态文件 静态文件由./public目录提供。可以通过设置:public选项指定其他位置: 请注意,URL中不包括公用目录名。文件./public/css/style.css作为example.com/css/style.css提供
请记住,在生产过程中,您可以让web服务器自动发送
index.html
,这样请求就永远不会到达Sinatra。这对于性能来说是更好的,因为您不必为了提供静态文本而遍历Sinatra/Rack堆栈,这正是Apache/Nginx所擅长的。您可以使用send\u file
助手来提供文件
require 'sinatra'
get '/' do
send_file File.join(settings.public_folder, 'index.html')
end
这将从配置为具有应用程序静态文件的任何目录提供
index.html
。您可以从公用文件夹托管它们,它们不需要路由
.
-- myapp.rb
`-- public
|-- application.css
|-- application.js
|-- index.html
`-- jquery.js
在myapp.rb中
set :public_folder, 'public'
get "/" do
redirect '/index.html'
end
链接到公用文件夹中的某个子文件夹
set :public_folder, 'public'
get "/" do
redirect '/subfolder/index.html'
end
/public中的所有内容都可以从“/whatever/bla.html”访问
示例:/public/stylesheets/screen.css
将通过“/stylesheets/screen.css”访问,无需路由gem提供了一系列功能。语法是甜蜜的:
serve '/js', from: '/app/javascripts'
<> p>当我仍然对Rails资产管道有问题时,我觉得我有更多的控制使用-但是大多数时候它只是用几行代码工作。 你可以考虑把<代码> index .html < /Cord>文件移到<代码>视图/索引.Erb < /C>,并且定义一个端点,比如:
get '/' do
erb :index
end
将文件放入
公用文件夹有一个限制。实际上,当您在根目录中时,“/”
路径正常工作,因为浏览器将设置css文件的相对路径,例如/css/style.css
,sinatra将在公共
目录中查找该文件。但是,如果您的位置是例如/user/create
,那么web浏览器将在/user/create/css/style.css
中查找您的css文件,并且将失败
作为一种解决方法,我添加了以下重定向以正确加载css文件:
get %r{.*/css/style.css} do
redirect('css/style.css')
end
更新的答案:
我把所有这些都联系在一起,却没有幸能够加载css、js…等内容。唯一加载的是index.html。。。其余的都是=>>404错误
我的解决方案:应用程序文件夹如下所示
index.rb
==>>Sinatra代码运行
require 'rubygems'
require 'sinatra'
get '/' do
html :index
end
def html(view)
File.read(File.join('public', "#{view.to_s}.html"))
end
公用文件夹
==>>包含其他所有内容…css、js、等等
user@user-SVE1411EGXB:~/sintra1$ ls
index.rb public
user@user-SVE1411EGXB:~/sintra1$ find public/
public/
public/index.html
public/about_us.html
public/contact.html
public/fonts
public/fonts/fontawesome-webfont.svg
public/fonts/fontawesome-webfont.ttf
public/img
public/img/drink_ZIDO.jpg
public/js
public/js/bootstrap.min.js
public/js/jquery.min.js
public/js/bootstrap.js
public/carsoul2.html
public/css
public/css/font-awesome-ie7.css
public/css/bootstrap.min.css
public/css/font-awesome.min.css
public/css/bootstrap.css
public/css/font-awesome.css
public/css/style.css
user@user-SVE1411EGXB:~/sintra1$
现在启动服务器,您将能够毫无问题地浏览静态页面
user@user-SVE1411EGXB:~/sintra1$ ruby index.rb
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Thin
>> Thin web server (v1.5.1 codename Straight Razor)
>> Maximum connections set to 1024
>> Listening on localhost:4567, CTRL+C to stop
在主rb文件中添加以下行
set :public_folder, 'public'
这将是正确的做法
set :public_folder, 'public'
我使用了静态设置,因为它的设置会影响公用文件夹的使用。您可以始终使用Rack::static
只需将“run”命令之前的这一行添加到“config.ru”中
use Rack::Static, :urls => [""], :root => 'public', :index => 'index.html'
我了解到访问是有效的。你可以使用WebBrick在几行中提供静态文件<代码>需要“webrick”;server=WEBrick::HTTPServer.new端口:1234;server.mount'/',WEBrick::HTTPServlet::FileHandler',www/;陷阱(“INT”){server.stop};server.start
然后运行ruby myapp.rb
。卸下Heroku的端口。将web:ruby myapp.rb
放在Procfile
中。评论不回答,因为这不是针对Sinatra的,但我认为它简化了依赖关系。哦,是的,duh。我将使用Erb然后使用清漆来兑现它。您如何在生产中配置它?我一直在搜索有关Sinatra和Rack交叉引用的文档,但找不到。基本上,如果用户只输入文件夹名,我希望index.html加载到任何有文件夹名的/public文件夹中。我认为较新的Sinatra应用程序使用set:public\u folder
,因此您将使用settings.public\u folder
而不是settings.public
我更新了使用settings.public\u folder的答案。较旧的应用程序可能仍然需要使用settings.public。您应该执行发送文件。展开路径('index.html',settings.public)
。这现在是不正确的。您应该将settings.public
替换为settings.public\u folder
以获取send\u文件。展开\u path('index.html',settings.public\u folder)
@zhirzhsend\u file
,它为您提供额外的文件。read
将整个文件读取到内存中。这可以是正常的,也可以是不正常的,这取决于文件的大小和并发请求的数量。@WayneConrad相反,send_file可以吗?或者它的行为是一样的?为什么它有4票?它没有回答在请求文件夹时如何显示默认文档的问题。如果public有许多嵌套文件夹(您不想为其创建路由),其中包含index.html文件,您希望成为默认文件夹,该怎么办?我已扩展了解决方案。我希望这有助于澄清,一切都可以公开访问,不需要路由,只需省略路径的“公共”部分。使用Heroku上的rackup,我必须使用set:public\u folder,“public”
。这是使它发挥作用的关键,尽管西纳特拉
use Rack::Static, :urls => [""], :root => 'public', :index => 'index.html'