【发布时间】:2017-09-27 18:38:58
【问题描述】:
我正在使用 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 我只是得到一个错误。
有没有办法重定向(不更改网址)或其他解决方案?
这不是How to change Iron's default 404 behaviour? 的重复,因为我试图在用户请求的静态资产不存在时进行处理,而不是在未定义路由时进行处理。
【问题讨论】:
-
由于这不是答案,Iron 确实有 static file middleware。
-
@squiguy 我认为这就是
Static::new在这个问题中的来源,但 OP 没有提供 minimal reproducible example 所以谁知道...
标签: rust static-files iron