每次网页 127.0.0.1:3000 被访问,number值+2,而不是+1? for example: number 显示 2,4,6,…
node程序如下
/**
* New node file
*/
var http=require('http');
var localTime=new Date();
var number =0;
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/html'});
res.write('<h1>Node.js</h1>');
res.end('<p>Hello World, you are the ' + number + ' vistior! </p>');
req.on('end',function(){
number++;
console.log("this webpage is accessed!");
console.log(localTime.toLocaleString());
});
}).listen(3000);
有何问题?
11 回复
thanks! 是favicon.ico请求所导致
- / 表示用户输入的客户端请求的目标URL地址,"/"代表用户的目标url地址为web应用程序的根目录.
- /favicon.ico表示为浏览器为页面在收藏夹中的显示图标.默认为favicon.ico.而自动发出的请求的目标URL地址. 我改为
/**
* New node file
*/
var http=require('http');
var localTime=new Date();
var number =0;
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/html'});
res.write('<h1>Node.js</h1>');
res.end('<p>Hello World, you are the ' + number + ' vistior! </p>');
req.on('end',function(){
console.log(req.url);
if(req.url !== '/favicon.ico'){
//todo
number++;
console.log("this webpage is accessed!");
console.log(localTime.toLocaleString());
}
});
}).listen(3000);