Function actix_web::web::resource

source ·
pub fn resource<T: IntoPatterns>(path: T) -> Resource
Expand description

Creates a new resource for a specific path.

Resources may have dynamic 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 dynamic 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 Path 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 Path object:

Examples

use actix_web::{web, App, HttpResponse};

let app = App::new().service(
    web::resource("/users/{userid}/{friend}")
        .route(web::get().to(|| HttpResponse::Ok()))
        .route(web::head().to(|| HttpResponse::MethodNotAllowed()))
);