pub fn resource<St: State, In: 'static, T: IntoPattern>(
path: T,
) -> Resource<St, In>Expand description
Create resource for a specific path.
Resources may have variable path segments. For example, a
resource with the path /a/{name}/c would match all incoming
requests with paths such as /a/b/c, /a/1/c, or /a/etc/c.
A variable segment is specified in the form {identifier},
where the identifier can be used later in a request handler to
access the matched value for that segment. This is done by
looking up the identifier in the Params object returned by
HttpRequest.match_info() method.
By default, each segment matches the regular expression [^{}/]+.
You can also specify a custom regex in the form {identifier:regex}.
For instance, to route GET-requests on any route matching
/users/{userid}/{friend} and store userid and friend in
the exposed Params object:
use ntex::web;
#[ntex::main]
async fn main() -> std::io::Result<()> {
web::server(async |_| {
web::App::new().service(
web::resource("/users/{userid}/{friend}")
.route(web::get().to(async || { web::HttpResponse::Ok() }))
.route(web::head().to(async || { web::HttpResponse::MethodNotAllowed() }))
)
})
.bind("127.0.0.1:59090", ntex::SharedCfg::default())?
.run()
.await
}