To create an HTTP server on a specific port and host in Rust, you can use the hyper
crate, which provides a high-level abstraction over HTTP and provides an easy-to-use API for creating servers.
Here's a simple example:
main.rs613 chars22 lines
This code creates an HTTP server that listens on 127.0.0.1:3000
, and handles all incoming requests by calling the handle_request
function. The handle_request
function just returns a simple "Hello, World!" response.
Note that the tokio
crate is used here to spawn a new asynchronous runtime to handle the server. In order to use the tokio
runtime, you need to add it to your Cargo.toml
dependencies:
77 chars4 lines
This example should give you a basic understanding of how to create an HTTP server in Rust using hyper
. From here, you can add more complex request handlers and middleware as needed.
gistlibby LogSnag