[英]Rack::Builder and Rack::TryStatic doesn't work
我有一個Middleman應用程序,我使用Rack::TryStatic
。
這是config.ru
。
use Rack::TryStatic,
root: 'build',
urls: %w[/],
try: ['.html', 'index.html', '/index.html']
run lambda{ |env|
four_oh_four_page = File.expand_path("../build/404.html", __FILE__)
[ 404, { 'Content-Type' => 'text/html'}, [ File.read(four_oh_four_page) ]]
}
我的理解是,當您使用config.ru
run
, map
或use
方法時,它們將轉換為Rack::Builder
對象。
我試過在Rack::Builder
對象中包裝這個配置,如下所示:
app = Rack::Builder.new do
use Rack::TryStatic,
root: 'build',
urls: %w[/],
try: ['.html', 'index.html', '/index.html']
run lambda{ |env|
four_oh_four_page = File.expand_path("../build/404.html", __FILE__)
[ 404, { 'Content-Type' => 'text/html'}, [ File.read(four_oh_four_page) ]]
}
end
run app
當我這樣做時,我得到所有請求的404頁面。
為什么這不起作用?
看起來當前發布的TryStatic
版本中TryStatic
一個已在master中修復的錯誤,其中try
數組在以導致中間件重新初始化的方式使用時丟失。
您可以通過使用to_app
確保應用程序僅初始化一次來避免這種to_app
:
run app.to_app
或者等效地使用app
而不是new
:
app = Rack::Builder.app do
# ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.