gpt4 book ai didi

ruby-on-rails - 使用 Rack 静态页面编写 404 错误页面路由

转载 作者:数据小太阳 更新时间:2023-10-29 08:50:40 24 4
gpt4 key购买 nike

如何将 config.ru 文件中的 404 错误页面映射到 Rack 静态页面(托管在 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 中的基本链接有帮助。

最佳答案

你应该使用 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

关于ruby-on-rails - 使用 Rack 静态页面编写 404 错误页面路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13776474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com