Plack::Builder 和 this answer 的概要说:
# in .psgi
use Plack::Builder;
my $app = sub { ... };
builder {
mount "/foo" => builder {
enable "Foo";
$app;
};
mount "/bar" => $app2;
mount "http://example.com/" => builder { $app3 };
};
我尝试了以下方法:
use Plack::Builder;
my $app1 = sub { return [200, ['Content-Type' => 'text/plain'], [ "Hello 1"] ]; };
my $app2 = sub { return [200, ['Content-Type' => 'text/plain'], [ "Hello 2"] ]; };
my $app3 = sub { return [200, ['Content-Type' => 'text/plain'], [ "Hello 3"] ]; };
builder {
mount "/a1" => builder { $app1 };
mount "http://myhost.com" => builder{ $app2 };
mount "/" => builder{ $app3 };
}
但是当试图用
plackup
运行它时得到:怎么了?
最佳答案
我没有看到文档中明确提到这一点,但是除了主机名之外,您还必须包含一个路径组件,例如http://myhost.com/foo
。改变
mount "http://myhost.com" => builder{ $app2 };
至
mount "http://myhost.com/" => builder{ $app2 };
(即主机
/
上的 myhost.com
)相关代码在 Plack::App::URLMap 中(
mount
只是调用了 Plack::App::URLMap 的 map
方法):if ($location =~ m!^https?://(.*?)(/.*)!) {
$host = $1;
$location = $2;
}
关于perl - 在 Plack::Builder 中安装 "hosts",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32894204/