Deno:请求被调用两次
Deno: request getting called twice
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)});
}
在上面的代码中,当我尝试点击 http://localhost:8000/ 时,我可以看到控制台记录了两次 'test- call'。
不确定为什么会这样。需要帮助才能理解。
您的代码实际上 运行 没问题。
如果您尝试使用 browser
访问 http://localhost:8000
,这是输出:
$ open web browser http://localhost:8000
test- call GET /
test- call GET /favicon.ico <- extra
但是,如果您使用 curl
访问它,那么您将只看到一个预期的输出:
$ curl localhost:8000
test- call GET /
要查看额外请求,请记录请求详细信息:
console.log('test- call', req.method, req.url)
希望对您有所帮助!
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)});
}
在上面的代码中,当我尝试点击 http://localhost:8000/ 时,我可以看到控制台记录了两次 'test- call'。
不确定为什么会这样。需要帮助才能理解。
您的代码实际上 运行 没问题。
如果您尝试使用 browser
访问 http://localhost:8000
,这是输出:
$ open web browser http://localhost:8000
test- call GET /
test- call GET /favicon.ico <- extra
但是,如果您使用 curl
访问它,那么您将只看到一个预期的输出:
$ curl localhost:8000
test- call GET /
要查看额外请求,请记录请求详细信息:
console.log('test- call', req.method, req.url)
希望对您有所帮助!