【问题标题】:How can I match the root path with a Warp filter?如何将根路径与 Warp 过滤器匹配?
【发布时间】:2020-07-20 20:37:23
【问题描述】:

我有一个使用 Warp 提供静态文件的简单 Web 服务器。我按照 Warp 文档中的示例获得以下信息:

let index_html = warp::path("index.html").map(handlers::index_html); // reply is handled in `handlers::`

let script_js = warp::path("script.js").map(handlers::script_js);

let routes = warp::get().and(index_html.or(script_js));

warp::serve(routes).run(([127, 0, 0, 1], 8000)).await;

这会在localhost:8000/index.htmllocalhost:8000/script.js 请求时返回文件。

我想从localhost:8000 而不是/index.html 提供索引文件,但我不确定如何使用warp::path 指定域根目录。我试过用

替换warp::path("index.html")
  • warp::path()
  • warp::path("")
  • warp::path("/")

但没有成功。

【问题讨论】:

    标签: filter rust


    【解决方案1】:

    要定位根路径,请使用warp::path::end()docs 的描述含糊不清。

    对于上面的示例,index_html 的代码将替换为:

    let index_html = warp::path::end().map( ... );
    

    【讨论】:

    • 另见path!宏,默认添加end()。然后你就可以使用path!()
    猜你喜欢
    • 2021-12-16
    • 1970-01-01
    • 2021-05-29
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多