Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

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

like image 417
brad Avatar asked Apr 07 '26 19:04

brad


1 Answers

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.

like image 165
alienhard Avatar answered Apr 10 '26 10:04

alienhard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!