var http = require('http').createServer(function (req,resp){
console.log("Started");
});
http.listen(1337);
我启动服务器,console.log不记录任何内容。我加载一个网页,每次加载时,都会收到console.log(“已启动”)消息。是什么给了什么?
答案 0 :(得分:0)
console.log()
在HTTP请求侦听器函数内运行,该函数仅在HTTP服务器收到请求时运行。创建服务器时,不会调用侦听器函数,因此console.log()
不会运行。
如果您希望消息在HTTP服务器上运行侦听,请在listen()
函数的回调中使用它。
server.listen(80, function() {
// this runs when the server has been started
});