[][src]Function ntex::web::resource

pub fn resource<T: IntoPattern, Err: ErrorRenderer>(path: T) -> Resource<Err>

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;

let app = 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() }))
);