为什么一个 Warp "hello world" 示例有效,而另一个则不起作用?



以下两个"你好世界";样本构建成功,但只有第一个结果在页面上显示";Hello World";第二个错误告诉我找不到页面。Cargo.toml文件对两者都是相同的。我正在访问IP127.0.0.1:3030

我尝试使用curl访问第二个,但它没有返回任何结果,并返回到提示。我第一次成功使用的浏览器是Microsoft Edge 84。

为什么第二个不起作用?

第一个

#![deny(warnings)]
use warp::Filter;
#[tokio::main]
async fn main() {
// Match any request and return hello world!
let routes = warp::any().map(|| "Hello, World!");
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;

第二个

use warp::Filter;
#[tokio::main]
async fn main() {
// GET /hello/warp => 200 OK with body "Hello, warp!"
let hello = warp::path!("hello" / String)
.map(|name| format!("Hello, {}!", name));
warp::serve(hello)
.run(([127, 0, 0, 1], 3030))
.await;
}

Cargo.toml

[package]
name = "warptest"
version = "0.1.0"
authors = ["user"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tokio = { version = "0.2", features = ["full"] }
warp = "0.2"

第二个示例响应路径/hello/string-argument上的请求。未配置为响应根路径/:

% curl -vvvv 'http://0.0.0.0:3030/'
*   Trying 0.0.0.0...
* TCP_NODELAY set
* Connected to 0.0.0.0 (127.0.0.1) port 3030 (#0)
> GET / HTTP/1.1
> Host: 0.0.0.0:3030
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 404 Not Found
< content-length: 0
< date: Mon, 24 Aug 2020 19:53:06 GMT
<
* Connection #0 to host 0.0.0.0 left intact
* Closing connection 0
% curl -vvvv 'http://0.0.0.0:3030/hello/world'
*   Trying 0.0.0.0...
* TCP_NODELAY set
* Connected to 0.0.0.0 (127.0.0.1) port 3030 (#0)
> GET /hello/world HTTP/1.1
> Host: 0.0.0.0:3030
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< content-type: text/plain; charset=utf-8
< content-length: 13
< date: Mon, 24 Aug 2020 19:54:01 GMT
<
* Connection #0 to host 0.0.0.0 left intact
Hello, world!* Closing connection 0

相关内容

最新更新