Other Data Servers

Learn how to spin up a server for special cases, such as rendering files, static HTML directories, and templating.

We'll cover the following...

File or directory server

We occasionally need a server for special cases, not just to serve web pages. For example, we might need to show other kinds of files, or let the user download them. For this use case, warp comes to our rescue again with a simple solution.

Press + to interact
main.rs
data.json
use warp::Filter;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
server_setup!(3); // Needed for this playground to work
let data = warp::get()
.and(warp::path::end())
.and(warp::fs::file("/usercode/data.json"));
println!("Server started on: http://127.0.0.1:3030");
warp::serve(data).run(([127, 0, 0, 1], 3030)).await;
Ok(())
}

In the code above, we pass the file /usercode/data.json to the response with warp::fs::file() in line 9.

The warp functionality can also serve a whole directory by mapping each ...