我认为这个问题在 chrome 版本 67.0.3396.87(32 位)中仍然存在,因为当我运行我的 nodeJS 脚本时,我看到了 2 个 console.log() 语句,一个能够打印出查询,另一个则不能,所以我更正了我的代码只能查看一次 console.log() 语句,这很简单,如果 request.url 的开头是 == (equal to)"/favicon.ico",我只需添加一个 return 语句代码和一切正常
以前的代码
var http = require('http');
var url = require('url');
http.createServer((request,response)=>{
var q = url.parse(request.url,true).query;
console.log(request.url);
console.log('hey there! we got a request from '+q.name+' !');
}).listen(8080);
输出是:
/?name=harshit
hey there we got a request from harshit !
/favicon.ico
hey there we got a request from undefined !
调试后的代码:
var http = require('http');
var url = require('url');
http.createServer((request,response)=>{
if(request.url == "/favicon.ico"){
return ;
}
var q = url.parse(request.url,true).query;
console.log(request.url);
console.log('hey there! we got a request from '+q.name+' !');
}).listen(8080);
输出 :
/?name=harshit
hey there we got a request from : harshit !