4

Plack::Builder的概要和这个答案说:

# 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 };
}

但是当尝试使用plackupgot 运行它时:

加载 /tmp/app.psgi 时出错:路径需要以 / 开头 /home/cw/.anyenv/envs/pleenv/versions/5.20.3/lib/perl5/site_perl/5.20.3/Plack/Builder.pm第 108 行。

怎么了?

4

1 回答 1

5

我没有在文档中明确提到这一点,但是除了主机名之外,您还必须包含一个路径组件,例如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;
}
于 2015-10-01T18:42:13.437 回答