简体   繁体   English

开始使用node.js进行播放。 我有个问题

[英]Started Playing with node.js. I have a question

I just created a HTTP server using this code fro the documentation: 我只是使用以下代码通过文档创建了一个HTTP服务器:

var sys = require("sys"),
    http = require("http");

http.createServer(function(request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.end("Hello World!");
    sys.puts('Connection');
}).listen(8080);

sys.puts("Server running at http://localhost:8080/");**

My question is, why when I go to localhost:8080 I got "connection" printed twice? 我的问题是,为什么当我进入localhost:8080时,会两次打印“连接”? is this a bug? 这是一个错误吗?

Your browser may be requesting the URL twice, once with a HEAD request and once with a GET request. 您的浏览器可能两次请求URL,一次是HEAD请求,一次是GET请求。 Try using a simple interface, like telnet : 尝试使用简单的界面,例如telnet

$ telnet localhost 8080
GET / HTTP/1.0

^]q

Leave a blank line after GET , and press Ctrl+] q Enter to get out. GET之后留空行,然后按Ctrl +] q Enter退出。

two requests are sent to the server. 两个请求被发送到服务器。 The first request is automatically made by the browser, which requests favicon.ico, and of course, the second request is for the URL (localhost:8080). 第一个请求是由浏览器自动发出的,它会请求favicon.ico,当然,第二个请求是针对URL(localhost:8080)的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM