3

我是 Mojolicious 的新手,我已经完成了各种工作,但是我遇到了一个问题,我在下面的最少代码中重新创建了这个问题。

问题很简单:我无法加载外部 CSS 和 JS 文件。令我惊讶的是,它给出了 404 错误,就好像它试图将这些静态文件作为路由提供一样。似乎没有其他人遇到这个问题,所以我显然做了一些(或错过了一些)愚蠢的事情。

有问题的文件位于相对于 perl 文件 (errorddemo.pl) 的 ./css 和 ./js 目录中。我尝试过使用和不使用前导“/”,以及我能想到的任何其他变体。

这是代码:

#!/usr/bin/env perl
use Mojolicious::Lite;
get '/' => sub {
    my $c = shift;                                                     
    $c->render('index');
};            
app->start;

__DATA__
@@ index.html.ep
<!DOCTYPE html>
<html>
    %= stylesheet '/css/bootstrap-3.3.2-dist/css/bootstrap.css'
</head>      
<body>
    <p>blah
    %= javascript '/js/jquery-2.1.3.js'
</body>     
</html>

当我运行morbo errordemo.pl并浏览到:3000 时,我得到了这个:

[Sun Jan 25 00:24:04 2015] [debug] GET "/".
[Sun Jan 25 00:24:04 2015] [debug] Routing to a callback.
[Sun Jan 25 00:24:04 2015] [debug] Rendering template "index.html.ep" from DATA section.
[Sun Jan 25 00:24:04 2015] [debug] 200 OK (0.005127s, 195.046/s).
[Sun Jan 25 00:24:04 2015] [debug] GET "/css/bootstrap-3.3.2-dist/css/bootstrap.css".
[Sun Jan 25 00:24:04 2015] [debug] Template "not_found.development.html.ep" not found.
[Sun Jan 25 00:24:04 2015] [debug] Template "not_found.html.ep" not found.
[Sun Jan 25 00:24:04 2015] [debug] Rendering inline template "3e3201ab0667c1fc7f39089209f0435c".
[Sun Jan 25 00:24:04 2015] [debug] Rendering inline template "b2d451b47e2053ce583cbfdf7bcc6006".
[Sun Jan 25 00:24:04 2015] [debug] 404 Not Found (0.045663s, 21.900/s).
[Sun Jan 25 00:24:04 2015] [debug] GET "/js/jquery-2.1.3.js".
[Sun Jan 25 00:24:04 2015] [debug] Template "not_found.development.html.ep" not found.
[Sun Jan 25 00:24:04 2015] [debug] Template "not_found.html.ep" not found.
[Sun Jan 25 00:24:04 2015] [debug] Rendering cached inline template "3e3201ab0667c1fc7f39089209f0435c".
[Sun Jan 25 00:24:04 2015] [debug] Rendering cached inline template "b2d451b47e2053ce583cbfdf7bcc6006".
[Sun Jan 25 00:24:04 2015] [debug] 404 Not Found (0.009863s, 101.389/s).

生成的 HTML 是:

<!DOCTYPE html>
<html>
    <link href="/css/bootstrap-3.3.2-dist/css/bootstrap.css" rel="stylesheet" />
</head>
<body>
    <p>blah
    <script src="/js/jquery-2.1.3.js"></script>
</body>

4

2 回答 2

7

相对于 lite 应用程序 ( errorddemo.pl ) ,静态文件./public提供。

您可以通过在以下位置修改数组引用来指定其他文件夹app->static->paths

push @{app->static->paths} => '.';
于 2015-01-25T01:10:01.840 回答
5

我不推荐 tempire 的建议,因为它会将您的完整项目文件夹(包括 errordemo.pl 和任何私有配置文件)暴露给公众。

将来,我建议您调查paths设置的内容:

warn join ":", @{app->static->paths};

这些有趣的部分会让您了解 Mojolicious 在哪里寻找静态文件和模板。

warn join ":", @{app->static->paths};
warn join ":", @{app->static->classes};
warn join ":", @{app->renderer->paths};
warn join ":", @{app->renderer->classes};

请注意,“路径”优先于“类”。您可以在此处阅读有关属性的更多信息:

http://mojolicio.us/perldoc/Mojolicious/Static

于 2015-01-25T15:54:47.180 回答