【问题标题】:CSS file is not read properly using actix-web and maud使用 actix-web 和 maud 无法正确读取 CSS 文件
【发布时间】:2022-02-12 05:35:29
【问题描述】:

CSS 无法通过 maud 与 actix-web 的链接 rel 属性工作。我在哪里犯了错误?

我认为我写链接rel属性的位置是正确的。

ma​​in.rs

use actix_files as fs;
use actix_web::{get, App, HttpServer, Result as AwResult};
use maud::{html, Markup};

#[get("/")]
async fn index() -> AwResult<Markup> {
    Ok(html! {
        head{
            link rel="stylesheet" href="/static/style.css"{};
        }
       title{"Help me"}

       p {"Blue text should be shown."}

    })
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .service(fs::Files::new("/static", ".").show_files_listing())
            .service(index)
    })
    .bind("0.0.0.0:80")?
    .run()
    .await?;
    Ok(())
}

style.css

p {
    color: blue;
    font-size: 50px;
}

【问题讨论】:

    标签: css rust href actix-web


    【解决方案1】:

    actix_files::Files的文档:

    第一个参数 (mount_path) 是提供静态文件的根 URL。

    第二个参数 (serve_from) 是磁盘上加载文件的位置。

    你使用/static作为第一个参数,所以你的文件将在http://0.0.0.0:80/static下,好吧。

    但作为第二个参数,您设置了.,即项目的根。这意味着您正在发布项目的整个源代码!例如,在http://0.0.0.0:80/static/src/main.rshttp://0.0.0.0:80/static/ 是挂载点,./src/main.rs 是本地文件),您将下载二进制文件的源代码,而您不希望这样。

    要解决您的问题,请改写:

    fs::Files::new("/static", "./static")
    

    当然,第二个参数中的./ 是可选的,但我喜欢它,因为它提醒您它是相对于当前工作目录的。

    【讨论】:

      猜你喜欢
      • 2012-03-09
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多