Skip to content

Instantly share code, notes, and snippets.

@akashsky44
Created October 17, 2018 13:18
Show Gist options
  • Save akashsky44/b8ceb510f73189cb22154d795d8d82f7 to your computer and use it in GitHub Desktop.
Save akashsky44/b8ceb510f73189cb22154d795d8d82f7 to your computer and use it in GitHub Desktop.
Pixel Code in Node js whenever you used insert in href of img tag
var fs = require('fs'),
http = require('http'),
url = require('url'),
img = fs.readFileSync(__dirname + '/stat.png'),
stats = {};
var collectStats = function(type) {
console.log('collectStats type=' + type);
if(!stats[type]) stats[type] = 0;
stats[type]++;
}
http.createServer(function(req, res){
var request = url.parse(req.url, true);
var action = request.pathname;
if (action == '/stat.png') {
collectStats(request.query.type);
res.writeHead(200, {'Content-Type': 'image/gif', 'Cache-Control': 'no-cache' });
res.end(img, 'binary');
} else {
res.writeHead(200, {'Content-Type': 'text/html' });
res.end('Stats server:<pre>' + JSON.stringify(stats) + '</pre>\n');
}
}).listen(8000, '127.0.0.1');
console.log('Server is running at http://127.0.0.1:8000');
// Uses Example
// * http://127.0.0.1:8000/ - shows the collected statistics
// * http://127.0.0.1:8000/stat.png?type=something - collecting statistics.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment