Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

ajax - Why does node hear two requests? (Favicon)

I've tried to make a simple API. If someone/something queries:

myIP:port/query

It should serve some result. However, I've noticed that (at least) when the query is made by a browser (Chrome), the server receives two requests. If the server simply is set up as follows:

http.createServer(function (request,result) {
    console.log(request.url);
    handleRequest(request,result);
}).listen(3000, '0.0.0.0');

It prints two request urls:

  • /query
  • /favicon.ico

I imagine that this means that the requesting client automatically draws an additional request hoping to load the favicon as well as the actual page.

  • Is this assumption correct?

  • Is there anything I can do when making the request to prevent this? Is it driven by chrome or would it also occur if I queried the page using ajax?

  • What is the best practice on the server side for filtering out the favicon request with minimal wasted effort by the server?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Your assumption is correct. Most browsers, upon first visit to a website, will issue a GET /favicon.ico if there is no favicon specified as a <link rel="icon" ... /> in the head of the HTML body.
  2. No, this is a client (browser) side implemented feature. There is nothing you can do server-side to prevent this behavior. The browser will not make an additional request to /favicon.ico if the request is made via AJAX, but it is likely that the browser has already made that request by this time (you have to have loaded the page to perform an AJAX request).
  3. As far as I know there are no server-side "best practices" for reducing the number of these requests. However, you could modify the template (or HTML file) you are serving to the client by explicitly including a <link ref="icon" ... /> statement in the head of the document.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...