2012-12-08 80 views
2

如何映射config.org文件中的404錯誤頁面機架靜態頁面(託管在heroku上)?寫404錯誤頁面路由與機架靜態頁面

到目前爲止,我有這在我的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的更多優勢。我沒有真正找到寶石的基本鏈接有幫助。

回答

3

你應該使用Rack::Builder,它會自動拋出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