CORS middleware can be used for Cross-Origin Resource Sharingopen in new window.

Since the browser will send Method::OPTIONS requests, it is necessary to increase the handling of such requests. You can add empty_handler to the root Router to handle this situation once and for all.

Config Cargo.toml

salvo = { version = "*", features = ["cors"] }

Sample Code

use salvo::prelude::*;
use salvo_extra::cors::Cors;

async fn hello() -> &'static str {

async fn main() {

    let cors_handler = Cors::builder()
        .with_allow_methods(vec!["GET", "POST", "DELETE"])

    let router = Router::with_hoop(cors_handler).get(hello).options(empty_handler);