This example sends a hard-coded response to the user when they send a server request.
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!")
}
When creating a new Iron
server in this example, expect
to catch any errors with a more descriptive error message. In production applications, handle the error produced (see the documentation for http()
).