Hey guys, I am trying out node.js, the following is the example web server from the documentation with an added counter and it prints the counter to the console when ever a client/browser requests the page.
The problem is, its being called twice when requested by the browser.
This is what I would expect would happen:
browser : Hello World 1
console : Counter 1
[reload page]
browser : Hello World 2
console : Counter 2
but this happens:
browser : Hello World 1
console : Counter 1
Counter 2
[reload page]
browser : Hello World 3
console : Counter 3
Counter 4
I run the code using the command line
$ node example.js
Here is the code:
var
http = require('http'),
counter = 0,
sys = require('util');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
counter++;
res.end('Hello World ' + counter + '\n');
sys.puts('Counter ' + counter);
}).listen(8000, "");
Any help is appreciated.
Thanks