Struct ntex::web::Route[][src]

pub struct Route<Err: ErrorRenderer = DefaultError> { /* fields omitted */ }
Expand description

Resource route definition

Route uses builder-like pattern for configuration. If handler is not explicitly set, default 404 Not Found handler is used.

Implementations

Create new route which matches any request.

Add method guard to the route.

App::new().service(web::resource("/path").route(
    web::route()
        .method(http::Method::CONNECT)
        .guard(guard::Header("content-type", "text/plain"))
        .to(|req: HttpRequest| async { HttpResponse::Ok() }))
);

Add guard to the route.

App::new().service(web::resource("/path").route(
    web::route()
        .guard(guard::Get())
        .guard(guard::Header("content-type", "text/plain"))
        .to(|req: HttpRequest| async { HttpResponse::Ok() }))
);

Set handler function, use request extractors for parameters.

use ntex::web;

#[derive(serde::Deserialize)]
struct Info {
    username: String,
}

/// extract path info using serde
async fn index(info: web::types::Path<Info>) -> String {
    format!("Welcome {}!", info.username)
}

fn main() {
    let app = web::App::new().service(
        web::resource("/{username}/index.html") // <- define path parameters
            .route(web::get().to(index))        // <- register handler
    );
}

It is possible to use multiple extractors for one handler function.

use ntex::web;

#[derive(serde::Deserialize)]
struct Info {
    username: String,
}

/// extract path info using serde
async fn index(path: web::types::Path<Info>, query: web::types::Query<HashMap<String, String>>, body: web::types::Json<Info>) -> String {
    format!("Welcome {}!", path.username)
}

fn main() {
    let app = web::App::new().service(
        web::resource("/{username}/index.html") // <- define path parameters
            .route(web::get().to(index))
    );
}

Trait Implementations

Responses given by the service

Errors produced by the service

Errors produced while building a service.

The Service value created by this factory

The future of the ServiceFactory instance.

Create and return a new service value asynchronously.

Map this service’s output to a different type, returning a new service of the resulting type. Read more

Map this service’s error to a different error, returning a new service.

Map this factory’s init error to a different error, returning a new service.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Convert Self to a ServiceFactory

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more