CSS not loading with Actix web

This is my relevant Rust code:

async fn index() -> Result<HttpResponse> {


<link rel="stylesheet" type="text/css" href="css/general.css">

File structure:

├── Cargo.lock
├── Cargo.toml
├── html
│   ├── css
│   │   └── general.css
│   ├── index.html
│   ├── login.html
│   └── register.html
└── src
    └── main.rs

When I run the code and go to the HTML gets loaded as expected but the CSS doesn't. I tried every path to the css file possible (relative from cargo run location etc) but none worked.

How to get the CSS to work?

You, as a web server, should also serve the css files as well. Browsers can only access what the web servers provides, they can't automatically search web server machine's file system.

I think it's better than to keep the front-end and back-end (Rust-code) totally separated than. And maybe let the two sides communicate using Ajax. Writing back-end in Rust seems totally different than in PHP I think.

Check https://actix.rs/docs/static-files/

But to be honest I'm not sure what's the best practice with Actix, in general static files are served through your web server and you connect your web server (for example Nginx) with your web app.

Serving statics from actix works just fine, there is no need for nginx (at least not just for that).

Right, I don't really know Actix, but in general you don't expose app servers to the world in production.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.