如何用Actix搭建Web服务器自动提供Gatsby部署的全部HTML文件
如何让Actix Web自动提供Gatsby部署的HTML文件
我先通过GoFiber实现了一个静态文件Web服务器,它可以自动提供Gatsby部署生成的全部HTML文件,运行正常,代码如下:
package main import ( "log" "github.com/gofiber/fiber/v2" ) func main() { app := fiber.New() app.Static("/", "./public") log.Fatal(app.Listen(":8000")) }
随后我尝试用Rust的Actix Web框架实现相同的静态文件服务,写出了如下代码:
use actix_files as fs; use actix_web::{App, HttpServer}; #[actix_web::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new().service( fs::Files::new("/", "./public") .show_files_listing() .use_last_modified(true), ) }) .bind("0.0.0.0:8000")? .run() .await }
但这段代码运行后只会显示文件列表页面,无法像GoFiber那样自动提供Gatsby的HTML文件——虽然手动点击index.html可以正常访问,且Actix的访问速度表现非常好。
解决方案
只需要在fs::Files的配置链中添加.index_file("index.html")方法,指定默认的索引文件即可实现自动提供HTML文件的功能,修改后的代码如下:
use actix_files as fs; use actix_web::{App, HttpServer}; #[actix_web::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new().service( fs::Files::new("/", "./public") .show_files_listing() .index_file("index.html") .use_last_modified(true), ) }) .bind("0.0.0.0:8000")? .run() .await }
内容的提问来源于stack exchange,提问作者Maicmi
相关产品推荐
相关产品推荐

