I have used the basic example of a NodeJS echo server and added an incremental counter:
var net = require('net')
var server = net.createServer()
var counter = 0
server.on('connection', function(conn) {
console.log('Got a connection...') // write to the console
conn.end('Hello client: ' + counter++ + '\n') // write to the client
conn.on('data', function(data) {
console.log('Data: ' + data) // log output contains header details
})
})
server.listen(8089)
Page load is accomplished by navigating to localhost:8089 on a browser – no special client-side connectivity code or anything else.
On initial load, the count is 0. On reloading the page the count is sometimes 2, 3, 4. On next load the count increments by a seemingly varying count between two and four. I initially thought that the port I was using may have been being pinged by some other process causing this. Changed the port – same result.
I’m seeing a log echo matching that number.
Can someone please explain why this is?
Most likely it’s the browser requesting
/favicon.ico. Curious as to why you’re using the lower level net library and not the http library to create the server. In using an http server, you can explicitly ignore the/favicon.icorequests and prevent them from affecting your counter.