【问题标题】:Writing 404 Error Page Route with Rack Static Page使用机架静态页面写入 404 错误页面路由
【发布时间】:2012-11-26 10:07:05
【问题描述】:

如何将 config.ru 文件中的 404 错误页面映射到机架静态页面(托管在 heroku 上)?

到目前为止,我的 config.ru 文件中有这个

use Rack::Static, 
  :urls => ["/css", "/images", "/fonts", "/js", "/robots.txt"],
  :root => "public"

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

我正在尝试做这样的事情:

if env["PATH_INFO"] =~ /^\/poller/
  [200, {"Content-Type" => "text/html"}, ["Hello, World!"]]
else
  [404, {"Content-Type" => "text/html"}, ["Not Found"]]
end

如何使用 Rack 实现这一目标?请分享您拥有的任何链接,我可以用来在 Rack 上学习更多高级内容。我并没有真正发现 gem 中的基本链接有帮助。

【问题讨论】:

    标签: ruby-on-rails ruby heroku rack middleware


    【解决方案1】:

    你应该使用Rack::Builder,它会自动为未映射的 URL 抛出 404:

    app = Rack::Builder.new do
    
      map '/poller' do
    
        use Rack::Static,
          :urls => ["/css", "/images", "/fonts", "/js", "/robots.txt"],
          :root => "public"
    
        run lambda { |env|
          [
            200, 
            {
              'Content-Type'  => 'text/html', 
              'Cache-Control' => 'public, max-age=86400' 
            },
            File.open('public/index.html', File::RDONLY)
          ]
        }
      end
    
    end.to_app
    
    run app
    

    【讨论】:

      猜你喜欢
      • 2017-11-04
      • 1970-01-01
      • 2013-08-31
      • 2020-06-21
      • 2011-10-24
      • 1970-01-01
      • 2022-12-09
      • 2016-10-20
      • 1970-01-01
      相关资源
      最近更新 更多