Nodejs web server calling requestListener twice when page is loaded

后端 未结 2 806
一整个雨季
一整个雨季 2020-12-30 03:52

The following is the example web server from the documentation, with an added counter. It prints the counter to the console whenever a client/browser requests the page.

2条回答
  •  天涯浪人
    2020-12-30 04:35

    In programming, when stuck, it's always handy to trace the code to better understand what's happening. The easiest way to do this is to put more debug/print statements in till you can see what's going on.

    Change the sys.puts line to:

    sys.puts('Counter ' + counter + " from " + req.url);
    

    I think you'll find that the 2nd request is the browser requesting the favicon for the site.

提交回复
热议问题