brad
brad

Reputation: 32345

Node.js - Why does my callback get called 3 times for each request?

This 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

Upvotes: 3

Views: 1916

Answers (1)

alienhard
alienhard

Reputation: 14692

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.

Upvotes: 7

Related Questions