#ntex #web

build clia-ntex-cors

Cross-origin resource sharing (CORS) for ntex applications

2 unstable releases

new 0.3.0 Mar 20, 2023
0.2.0 Mar 5, 2022

#105 in Build Utils

26 downloads per month

MIT license

804 lines

Cors Middleware for ntex framework

Documentation & community resources


Cross-origin resource sharing (CORS) for ntex applications

CORS middleware could be used with application and with resource. Cors middleware could be used as parameter for App::wrap(), Resource::wrap() or Scope::wrap() methods.


use ntex_cors::Cors;
use ntex::{http, web};
use ntex::web::{App, HttpRequest, HttpResponse};

async fn index(req: HttpRequest) -> &'static str {
    "Hello world"

async fn main() -> std::io::Result<()> {
    web::server(|| App::new()
            Cors::new() // <- Construct CORS middleware builder
              .allowed_methods(vec!["GET", "POST"])
              .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
              .route(web::head().to(|| async { HttpResponse::MethodNotAllowed() }))

In this example custom CORS middleware get registered for "/index.html" endpoint.

Cors middleware automatically handle OPTIONS preflight request.


~373K SLoC