Sinatra中的静态页面路由(Ruby)

JP.*_*JP. 13 ruby routing sinatra

你可以通过将它们放入public/(默认情况下)来为Sinatra提供静态文件- 我现在有一个index.html在那里,但是如何将根点指向该文件而不必将其解析为模板?

要清楚,我可以/index.html成功访问,并且我想路由/到相同的静态文件,但没有重定向.知道怎么做吗?

Pab*_*dez 20

可能一个更好的答案最终会到来,直到那时这是我的镜头.

如果这不是你想要的:

get '/' do
  redirect '/index.html'
end
Run Code Online (Sandbox Code Playgroud)

你可能会这样做:

get '/' do
  File.new('public/index.html').readlines
end
Run Code Online (Sandbox Code Playgroud)

我会选择第一个,但不确定为什么要避免重定向


小智 6

只需enable :static在app类中设置即可.像这样:

class App < Sinatra::Base
  # Set the app root; this is where the 'public' and 'views'
  # directories are found (by default).
  set :root, File.expand_path("#{File.dirname(__FILE__)}/../app")

  # Allow the app to serve static files from the 'public' directory in :root
  enable :static
end
Run Code Online (Sandbox Code Playgroud)