【问题标题】:Mojolicious gives 404 when getting JS and CSSMojolicious 在获取 JS 和 CSS 时给出 404
【发布时间】:2015-03-23 19:06:02
【问题描述】:

我是 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>

【问题讨论】:

    标签: perl mojolicious mojolicious-lite


    【解决方案1】:

    Static files./public 提供,相对于 lite 应用 (errorddemo.pl)。

    您可以通过修改app-&gt;static-&gt;paths处的数组引用来指定其他文件夹:

    push @{app->static->paths} => '.';
    

    【讨论】:

    • 谢谢——我知道我一定错过了一些基本的东西。我已将我的 css 和 js 文件夹移至公共文件夹,现在它可以工作了。 'public' 目录的使用记录在 rendering 下,毫无疑问在其他地方也是如此。
    • 我经常认为调试消息“.../public/js/... 不存在”会有所帮助。也许我应该这样做:)
    【解决方案2】:

    我不推荐 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

    【讨论】:

    • 好点。我只想知道 js 和 css 文件应该(通常)放在public 内。我并没有真正考虑过将当前目录添加到app-&gt;static-&gt;paths 的含义,但我同意这是一个坏主意。感谢您提供有关显示正在使用的路径值的提示。
    猜你喜欢
    • 2018-07-20
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2014-09-27
    相关资源
    最近更新 更多