【发布时间】:2019-05-11 00:40:03
【问题描述】:
我有以下 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("Enter into the 3000 port");
在代码中两次,但我不明白为什么它在单个请求中调用了两次,当我再次发送请求时,它再次向我显示了一些输出,任何人都可以解释。
【问题讨论】:
-
因为您告诉它在每个请求上记录两次相同的消息。使用
return res.end(message);和/或删除额外的日志。 -
@Luis Estevez 您能否在此详细解释一下,谢谢。 :)
-
是因为你的浏览器被调用了2次。当你通过 CURL 之类的东西请求时,它应该打印一次。
标签: javascript node.js http nodes node-modules