Node.js - Why does my callback get called 3 times for each request?
Th开发者_如何学编程is is my VERY FIRST node app. I'm literally just starting to piece through the API to see what it's all about. I'm immediately confused by the following server code and my console output. Can someone explain why my console.log happens 3 times on a browser refresh?
var http = require('http');
http.createServer(function(request, response){
response.writeHead(200, {'Content-Type': 'application/json'});
response.end("{blah: 1234}");
console.log("Hello!");
}).listen(3000, '127.0.0.1');
Output from a single refresh in the browser is:
Hello!
Hello!
Hello!
What am I missing?
OSX 10.5, Node 0.4.3
Most likely your browser is actually sending these requests.
Change console.log("Hello!")
to console.log(request.url)
to see what the paths of those requests are.
With Chrome I get only two requests, one for /
and one for /favicon.ico
.
精彩评论