Deno:请求被调用两次

Pun*_*wda 1 javascript deno

import { serve } from "https://deno.land/std@0.50.0/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
// listing to  requests 
for await (const req of s) {
    const res = await fetch('https://api.github.com/users')
    let data = await res.json()
    console.log('test- call')
    req.respond({ body:JSON.stringify(data)});
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,当我尝试点击http://localhost:8000/时,我可以看到控制台记录两次“test-call”。

不知道为什么会发生这种情况。需要帮助才能理解。

cvn*_*vng 5

您的代码实际上运行良好。

如果您尝试http://localhost:8000使用 a进行访问browser,则输出如下:

$ open web browser http://localhost:8000
test- call GET /
test- call GET /favicon.ico <- extra
Run Code Online (Sandbox Code Playgroud)

但是,如果您使用curlthen 访问它,您将只看到一个预期的输出:

$ curl localhost:8000
test- call GET /
Run Code Online (Sandbox Code Playgroud)

要查看额外请求,请记录请求详细信息:

console.log('test- call', req.method, req.url)
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!