gpt4 book ai didi

javascript - Nodejs Http Server api 执行流程请解释一下?

转载 作者:行者123 更新时间:2023-12-03 00:34:53 25 4
gpt4 key购买 nike

我有以下 Nodejs 程序

var http = require('http');
var url = require('url');
var server = http.createServer((req,res)=>{
console.log("Enter into the 3000 port");
res.end("hello world")
console.log("Enter into the 3000 port");
}).listen(3000,()=>{console.log("the server is listen to the port 3000");});

我在浏览器中加载 localhost:3000 时运行这些代码,当我编写 console.log("Enter into the 3000 port"); 来检查内部执行情况时,我得到了以下输出。输出:

the server is listen to the port 3000
Enter into the 3000 port
Enter into the 3000 port
Enter into the 3000 port
Enter into the 3000 port

但是我已经写了代码console.log("进入3000端口");在代码中两次,但我不明白为什么它在单个请求上调用两次,当我再次发送请求时,它再次向我显示一些输出,任何人都可以解释。

最佳答案

var http = require('http');
var url = require('url');
var server = http.createServer((req,res)=>{
if (req.url === '/favicon.ico') { return } // if you don't serve this hit
console.log(req.url);
console.log("Enter into the 3000 port");
res.end("hello world")
console.log("Enter into the 3000 port");
}).listen(3000,()=>{console.log("the server is listen to the port 3000");})

enter image description here

most of the browsers look for *favicon.ico* automatically you can avoid if you want

code

关于javascript - Nodejs Http Server api 执行流程请解释一下?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53700156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com