【问题标题】:How to write config.ru for Rack static site?如何为 Rack 静态站点编写 config.ru?
【发布时间】:2013-09-29 12:24:05
【问题描述】:

我的config.ru

require 'rack'
use Rack::Static, :root => '_site'

但是当我运行 rackup 时出现错误

/usr/local/share/gems/gems/rack-1.5.2/lib/rack/builder.rb:133:in `to_app': 缺少运行或映射语句 (RuntimeError)

我想在根 URL 的文件夹 _site_ 中托管文件

【问题讨论】:

    标签: ruby rack


    【解决方案1】:

    Rack::Static 是一个使用Rack::File 的中间件,它是一个应用程序。如果您所做的只是提供静态文件,您可以直接运行 Rack::File

    # note 'run' not 'use'
    run Rack::File.new('_site')
    

    【讨论】:

    • 这会运行,但找不到我的index.htmls。我想请求例如。 /CV 重定向到/CV/,然后被_site/CV/index.html 服务。
    【解决方案2】:

    您的config.ru 的问题在于它缺少run 命令,而您总是需要一个。正如马特建议的那样,您可以使用 Rack::File 中间件。

    但是,如果您想保留 Rack::Static 功能,那么您可以执行类似的操作来提供索引文件并在其前面放置 Rack::Static 中间件,以提供来自 _site_ 的任何文件。

    use Rack::Static,
      :urls => ["/"],
      :root => "_site_"
    
    run lambda { |env|
      [
        200,
        {
          'Content-Type'  => 'text/html',
          'Cache-Control' => 'public, max-age=86400'
        },
        File.open('public/index.html', File::RDONLY)
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-17
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      • 2019-03-20
      • 2014-05-04
      • 2010-09-12
      • 2023-03-21
      相关资源
      最近更新 更多