pub struct Route<'a, State> { /* private fields */ }
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§
Source§impl<'a, State: Clone + Send + Sync + 'static> Route<'a, State>
impl<'a, State: Clone + Send + Sync + 'static> Route<'a, State>
Sourcepub fn at<'b>(&'b mut self, path: &str) -> Route<'b, State>
pub fn at<'b>(&'b mut self, path: &str) -> Route<'b, State>
Extend the route with the given path
.
Sourcepub fn strip_prefix(&mut self) -> &mut Self
Available on unstable
only.
pub fn strip_prefix(&mut self) -> &mut Self
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.
Sourcepub fn with<M>(&mut self, middleware: M) -> &mut Selfwhere
M: Middleware<State>,
pub fn with<M>(&mut self, middleware: M) -> &mut Selfwhere
M: Middleware<State>,
Apply the given middleware to the current route.
Sourcepub fn reset_middleware(&mut self) -> &mut Self
pub fn reset_middleware(&mut self) -> &mut Self
Reset the middleware chain for the current route, if any.
Sourcepub fn nest<InnerState>(&mut self, service: Server<InnerState>) -> &mut Self
pub fn nest<InnerState>(&mut self, service: Server<InnerState>) -> &mut Self
Nest a Server
at the current path.
Sourcepub fn serve_dir(&mut self, dir: impl AsRef<Path>) -> Result<()>
pub fn serve_dir(&mut self, dir: impl AsRef<Path>) -> Result<()>
Serve a directory statically.
Each file will be streamed from disk, and a mime type will be determined based on magic bytes.
§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(())
}
Sourcepub fn serve_file(&mut self, file: impl AsRef<Path>) -> Result<()>
pub fn serve_file(&mut self, file: impl AsRef<Path>) -> Result<()>
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
Sourcepub fn method(&mut self, method: Method, ep: impl Endpoint<State>) -> &mut Self
pub fn method(&mut self, method: Method, ep: impl Endpoint<State>) -> &mut Self
Add an endpoint for the given HTTP method
Sourcepub fn all(&mut self, ep: impl Endpoint<State>) -> &mut Self
pub fn all(&mut self, ep: impl Endpoint<State>) -> &mut Self
Add an endpoint for all HTTP methods, as a fallback.
Routes with specific HTTP methods will be tried first.
Sourcepub fn head(&mut self, ep: impl Endpoint<State>) -> &mut Self
pub fn head(&mut self, ep: impl Endpoint<State>) -> &mut Self
Add an endpoint for HEAD
requests
Sourcepub fn post(&mut self, ep: impl Endpoint<State>) -> &mut Self
pub fn post(&mut self, ep: impl Endpoint<State>) -> &mut Self
Add an endpoint for POST
requests
Sourcepub fn delete(&mut self, ep: impl Endpoint<State>) -> &mut Self
pub fn delete(&mut self, ep: impl Endpoint<State>) -> &mut Self
Add an endpoint for DELETE
requests
Sourcepub fn options(&mut self, ep: impl Endpoint<State>) -> &mut Self
pub fn options(&mut self, ep: impl Endpoint<State>) -> &mut Self
Add an endpoint for OPTIONS
requests
Sourcepub fn connect(&mut self, ep: impl Endpoint<State>) -> &mut Self
pub fn connect(&mut self, ep: impl Endpoint<State>) -> &mut Self
Add an endpoint for CONNECT
requests