简单的 Hello Server

此示例在发送服务器请求时向用户发送硬编码响应。

extern crate iron;

use iron::prelude::*;
use iron::status;

// You can pass the handler as a function or a closure. In this
// case, we've chosen a function for clarity.
// Since we don't care about the request, we bind it to _.
fn handler(_: &mut Request) -> IronResult<Response> {
    Ok(Response::with((status::Ok, "Hello, Stack Overflow")))
}

fn main() {
    Iron::new(handler).http("localhost:1337").expect("Server failed!")
}

在此示例中创建新的 Iron 服务器时,expect 会使用更具描述性的错误消息来捕获任何错误。在生产应用程序中,处理产生的错误 (请参阅 http()文档 )。