Handling of favicon. ico requests in node. js _ node. js

Source: Internet
Author: User
This article records that two requests will appear when you use node. js to request favican. ico in the project, wasting resources. After some improvement, record the process and pay attention to it later. The Code is as follows:

Var http = require ("http ");
Var server = http. createServer ();
Server. on ("request", function (req, res) {5 console. log (req. url );
Res. end ();
Server. listen (1337, " ");

Such code will have two requests during the request:

In the first example, the URL address is the target URL of the client request entered by the user. "/" indicates that the target url address is the root directory of the web application.

The second target URL address is the display icon of the page in the favorites folder. The default value is favicon. ico. The target URL address of the automatically sent request.

You can modify the preceding code to block such requests.

The Code is as follows:

Var http = require ("http ");
Var server = http. createServer ();
Server. on ("request", function (req, res ){
If (req. url! = "/Favicon. ico ")
Console. log (req. url );
Res. end ();

The solution is simple, but practical. Let's record it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.