A static file is an asset served directly to clients without generating per-request content. In an Actix application, files such as HTML, CSS, and images can be mapped to a filesystem directory. section 7.2 section 8.2
A typical Actix setup serves static files by:
- Adding the actix-files dependency alongside actix-web. section 7.2
- Creating a static directory containing files such as static-web-page.html, stylesheets, or background images. section 7.2 section 8.2
- Registering fs::Files::new("/static", "./static") so GET requests beginning with /static resolve to files on disk. section 7.2
- Referencing an asset from HTML with a URL such as /static/tutor-styles.css or /static/background.jpg. section 8.2 section 7.5
Serving a static page
Place static-web-page.html under ./static, register the /static route, start the server with cargo run --bin static, and open http://localhost:8080/static/static-web-page.html. section 7.2
use actix_files as fs; #1
use actix_web::{error, web, App, Error, HttpResponse, HttpServer, Result};
#[actix_web::main]
async fn main() -> std::io::Result<()> { #2
let addr = env::var("SERVER_ADDR").unwrap_or_else(|_|
"127.0.0.1:8080".to_string());
println!("Listening on: {}, open browser and visit have a try!",addr);
HttpServer::new(|| {
App::new().service(fs::Files::new(
"/static", "./static").show_files_listing()) #3
})
.bind(addr)? #4
.run() #5
.await #6
}
Shows the Actix configuration that maps the /static URL prefix to the ./static directory and starts the server. section 7.2





