开发者

Serving static files in Sinatra... with beautiful routes?

开发者 https://www.devze.com 2023-03-01 12:20 出处:网络
Assuming I have a directory structure similar to: path_to_file/one/index.html How can I set my sinatra app to be routed to

Assuming I have a directory structure similar to:

path_to_file/one/index.html

How can I set my sinatra app to be routed to

mysite.com/path_to_file/one/

and have the previously mentioned file to render? path_to开发者_JAVA百科_file will always stay the same, but there will be different folders (two, three, etc.) inside it.

I've tried the following:

get '/path_to_file/:number' do
  File.read(File.join('path_to_file', "#{params[:number]}", "index.html"))
end

but then the e.g. javascript file linked from index.html doesn't render correctly.


Got it!

get '/path_to_file/:number/:file' do
  File.read(File.join('path_to_file', "#{params[:number]}", "#{params[:file]}"))
end

get '/path_to_file/:number' do
  File.read(File.join('path_to_file', "#{params[:number]}", "index.html"))
end

Order is important, since if these two methods are reversed, get '/path_to_file/:number' becomes a superset of get '/path_to_file/:number/:file'.


Just a thought, but you could setup your server software, Apache, nginx, whatever it is you're using, to serve .css and .js and image files from a different location.

0

精彩评论

暂无评论...
验证码 换一张
取 消