axum路由
use axum::{Router, routing::get};
// our router
let app = Router::new()
.route("/", get(root))
.route("/foo", get(get_foo).post(post_foo))
.route("/foo/bar", get(foo_bar));
// which calls one of these handlers
async fn root() {}
async fn get_foo() {}
async fn post_foo() {}
async fn foo_bar() {}
向路由器添加另一个路由。
path是由 / 分隔的路径段字符串。每个部分 可以是静态、捕获或通配符。 method_router is the MethodRouter that should receive the request if the path matches path. method_router will commonly be a handler wrapped in a method router like get. See handler for more details on handlers. method_router is the MethodRouter that should receive the request if the path matches path. method_router will commonly be a handler wrapped in a method router like get. See handler for more details on handlers. method_router 是 MethodRouter,如果路径与路径匹配,则应接收请求。 method_router 通常是一个处理程序,包裹在像 get 这样的方法路由器中。 有关处理程序的更多详细信息,请参阅处理程序。
静态路径 例子:
/ /foo /users/123 如果传入的请求与路径完全匹配,则相应的服务将 名叫。
捕获 路径可以包含与任何单个段匹配的段,并且 将存储在 捕获的值。/:keykey
例子:
/:key /users/:id /users/:id/tweets 可以使用路径提取捕获。查看其 文档了解更多详细信息。
无法创建仅匹配某些类型(如数字或数字)的细分 正则表达式。您必须在处理程序中手动处理它。
MatchedPath 可用于提取匹配的 路径而不是实际路径。
通配符 路径可以以匹配所有段的结尾,并将存储段 在 捕获。/*keykey
例子:
/*key /assets/*path /:id/:repo/*tree 请注意,这与空段不匹配。因此:/*key
/*key不匹配,但确实匹配,等。//a/a/ /x/*key不匹配或但匹配 、 等。/x/x//x/a/x/a/ 通配符捕获也可以使用 Path 提取。 请注意,不包括前导斜杠,即用于路由和 的值将为 的路径。/foo/*rest/foo/bar/bazrestbar/baz