Skip to main content

scope

Function scope 

Source
pub fn scope<St: State, In: 'static, T: IntoPattern>(path: T) -> Scope<St, In>
Expand description

Configure scope for common root path.

Scopes collect multiple paths under a common path prefix. Scope path can contain variable path segments as resources.

use ntex::web;

#[ntex::main]
async fn main() -> std::io::Result<()> {
    web::server(async |_| {
        web::App::new().service(
            web::scope("/{project_id}")
                .service(web::resource("/path1").to(async || { web::HttpResponse::Ok() }))
                .service(web::resource("/path2").to(async || { web::HttpResponse::Ok() }))
                .service(web::resource("/path3").to(async || { web::HttpResponse::MethodNotAllowed() }))
            )
   })
   .bind("127.0.0.1:59090", ntex::SharedCfg::default())?
   .run()
   .await
}

In the above example, three routes get added:

  • /{project_id}/path1
  • /{project_id}/path2
  • /{project_id}/path3