gpt4 book ai didi

perl - Mojolicious 在获取 JS 和 CSS 时给出 404

转载 作者:行者123 更新时间:2023-12-04 19:27:25 24 4
gpt4 key购买 nike

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

最佳答案

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

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

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

关于perl - Mojolicious 在获取 JS 和 CSS 时给出 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28131991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com