我正在使用 Iron 为 React 站点提供服务。如果文件或目录不存在,我试图让它为 index.html 服务。
fn staticHandler(req: &mut Request) -> IronResult<Response> {
let url = Url::parse("http://localhost:1393").unwrap();
let getFile_result = Static::handle(&Static::new(Path::new("../html")), req);
match getFile_result {
Ok(_) => getFile_result,
Err(err) => {
Static::handle(
// returns 404 error - ../html/index.html returns 500
&Static::new(Path::new("localhost:1393/index.html")),
req,
)
}
}
}
如果我去 localhost:1393 我会得到我的索引页面 如果我去 localhost:1393/not-a-directory 我只是得到一个错误。
有没有办法重定向(不更改网址)或其他解决方案?
这不是如何更改 Iron 的默认 404 行为的副本?因为我试图在用户请求的静态资产不存在时进行处理,而不是在未定义路由时进行处理。