Struct tide::Route [−][src]
pub struct Route<'a, State> { /* fields omitted */ }
Expand description
A handle to a route.
All HTTP requests are made against resources. After using Server::at
(or
Route::at
) to establish a route, the Route
type can be used to
establish endpoints for various HTTP methods at that path. Also, using
nest
, it can be used to set up a subrouter.
Implementations
Extend the route with the given path
.
This is supported on unstable
only.
unstable
only.Treat the current path as a prefix, and strip prefixes from requests.
This method is marked unstable as its name might change in the near future.
Endpoints will be given a path with the prefix removed.
Apply the given middleware to the current route.
Reset the middleware chain for the current route, if any.
Nest a Server
at the current path.
Note
The outer server always has precedence when disambiguating
overlapping paths. For example in the following example /hello
will
return “Unexpected” to the client
#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
let mut app = tide::new();
app.at("/hello").nest({
let mut example = tide::with_state("world");
example
.at("/")
.get(|req: tide::Request<&'static str>| async move {
Ok(format!("Hello {state}!", state = req.state()))
});
example
});
app.at("/*").get(|_| async { Ok("Unexpected") });
app.listen("127.0.0.1:8080").await?;
Ok(())
}
Serve a directory statically.
Each file will be streamed from disk, and a mime type will be determined based on magic bytes.
Security
This handler ensures no folders outside the specified folder can be
served, and attempts to access any path outside this folder (no matter
if it exists or not) will return StatusCode::Forbidden
to the caller.
Examples
Serve the contents of the local directory ./public/images/*
from
localhost:8080/images/*
.
#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
let mut app = tide::new();
app.at("/images/*").serve_dir("public/images/")?;
app.listen("127.0.0.1:8080").await?;
Ok(())
}
Serve a static file.
The file will be streamed from disk, and a mime type will be determined based on magic bytes. Similar to serve_dir
Add an endpoint for the given HTTP method
Add an endpoint for all HTTP methods, as a fallback.
Routes with specific HTTP methods will be tried first.
Add an endpoint for DELETE
requests
Add an endpoint for OPTIONS
requests
Add an endpoint for CONNECT
requests
Add an endpoint for PATCH
requests