德诺 |响应发生两次

deno | response happens two times

我是 deno 的新手,创建了一个简单的代码来响应我的请求。 我在那边放了一个计数器,看看响应重复了多少次,我每次都得到两次。

我的代码让服务器对每个请求响应两次有什么问题?

import { serve } from "https://deno.land/std@0.53.0/http/server.ts";
  const s = serve({ port: 8891 });
  var counter=0;
  console.log("http://localhost:8891/");
  for await (const req of s) {
        req.respond({ body: "<h1>" + counter + "</h1>" });
                                counter ++;
  }

网络浏览器对计数器的响应: 2,4,6,...

如果您在浏览器上执行该操作,浏览器会向 /favicon 发出额外的请求,这就是您认为服务器响应两次的原因。

如果您 运行 使用诸如 curl 之类的 HTTP 客户端,您将获得预期的行为。

您可能需要检查 URL 并根据它是 / 还是 /favicon 做出一件事或另一件事。