我尝试了以下内容:
let mut router = Router:new();
router.get("/hello", |_: &mut Request| {
Ok(Response::with((status::Ok, "hello")))
}, "/hello");
let mut mount = Mount::new();
mount.mount("/", router)
.mount("/", Static::new(Path::new("src/public/")));
但是当我尝试访问/hello
时,会导致“没有此类文件或目录”错误:
Request {
url: Url { generic_url: "http://localhost:10800/hello" }
method: Get
remote_addr: V4(127.0.0.1:57260)
local_addr: V4(127.0.0.1:10800)
}
Error was: Error { repr: Os { code: 2, message: "No such file or directory" } }
答案 0 :(得分:1)
根本不需要使用Router
;只需将处理程序直接传递给mount
:
extern crate iron;
extern crate mount;
extern crate staticfile;
use iron::prelude::*;
use iron::status;
use mount::Mount;
use staticfile::Static;
use std::path::Path;
fn main() {
let hello = |_: &mut Request| {
Ok(Response::with((status::Ok, "hello")))
};
let mut mount = Mount::new();
mount.mount("/hello", hello)
.mount("/", Static::new(Path::new("src/public/")));
let _server = Iron::new(mount).http("localhost:3000").unwrap();
println!("On 3000");
}
$ curl localhost:3000/animal
cow
$ curl localhost:3000/hello
hello