Home  >  Article  >  Web Front-end  >  Favicon.ico request problem handling in node.js_node.js

Favicon.ico request problem handling in node.js_node.js

WBOY
WBOYOriginal
2016-05-16 16:26:472640browse

Copy code 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,"127.0.0.1");

Such code will have two requests when requesting:

The first URL address is the target URL address of the client request entered by the user, and "/" means that the user's target URL address is the root directory of the web application.

The second target URL address asks the browser to display the icon of the page in the favorites. The default is favicon.ico. And the target URL address of the automatically issued request.

You can modify the above code to block such requests

Copy code 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 very simple but very practical, friends, please record it too.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn