2014-12-19 106 views
-1

我是新来的服务器端JavaScript所以Node.js。好了,在这里我想一些很简单的事情,如node.js简单的Web服务器请求生命周期

var http = require("http"); 

http.createServer(function(request, response) { 
    console.log("node server"); 
    response.writeHead(200, {"Content-Type": "text/plain"}); 
    response.write("Hello World"); 
    response.end(); 
}).listen(2424); 

正如我看到的结果是,每当我发出请求到服务器console.log("node server");执行两次。为什么会发生,节点(或http.createServer)如何提供请求?

+0

那么,记录请求。如果你从浏览器调用你的服务器,很可能其中之一是favicon。 –

+0

@dystroy两个时间if(request.path!='/ favicon')为真 – Riki

+0

记录请求,不要做这样的测试! –

回答

0

默认情况下,所有对页面的请求也会要求收藏图标。

要检查单页访问中请求的资源,请尝试使用其他console.log。

console.log(request.path); 

只是一个预感,但这可能是最可能的原因。

+0

两个时间'if(request.path!='/ favicon')'为真 – Riki

+0

道歉,我误解了被调用的路径,我修改了一下我的答案。你可以尝试记录request.path并看看它产生了什么? – irysius