我有一个简单的sinatra应用程序.我想要做的就是使用它作为包装器来在特定路由上提供静态HTML文件.我的目录结构如下所示:
/directory
myhtmlfile.html
app.rb
Run Code Online (Sandbox Code Playgroud)
我的app.rb
文件看起来像这样:
require 'sinatra'
get '/myspecialroute' do
html :myhtmlfile # i know html is not a method, but this is what I would like to do
end
Run Code Online (Sandbox Code Playgroud)
我怎么写这个,以便我可以保持我的HTML文件一个简单的HTML文件,但在特殊的路线上提供?
多亏了这一点,我学到了几种不同的方法:
get '/myspecialroute' do
File.read('myhtmlfile.html')
end
Run Code Online (Sandbox Code Playgroud)
这将打开,读取,关闭,然后将文件作为字符串返回.
或者有一个辅助函数来使这个更清洁:
get '/myspecialroute' do
send_file 'myhtmlfile.html'
end
Run Code Online (Sandbox Code Playgroud)
Ste*_*eve 40
send_file是否符合您的要求?
例如
get '/myspecialroute' do
send_file 'special.html'
end
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
17699 次 |
最近记录: |