Node.js is also used as an extremely efficient Webserver, so why not leverage the efficiency for a typical adserving web-beacon logging.
The node service has to do the following
- Data Logging
- Serving a 1×1 pixel image
Data Logging
Select through a list of available connection providers available for node.js through npm
Image Serving
Here you have 2 options, either read a file and output the buffer like below
... var img = fs.readFileSync('./log.gif'); res.writeHead(200, {'Content-Type': 'image/gif' }); res.end(img, 'binary'); ...
or you can read the content of the small file into a buffer, convert to a hexadecimal string, and save hexadecimal string as a javascript variable, and then at runtime convert the hex to a buffer and flush out the buffer as image/[type].
This shall greatly help remove all the file IO while serving the images.
to read the image as a buffer,
fs = require('fs'); imgBuffer = fs.readFileSync('1x1.gif'); // Convert the buffer to hex imgHex = imgBuffer.toString('hex');
Following sample code can then be added to the beacon script
//The hex string can now be directly added to the beacon script, simply as .. var imgHex = '47494638396101000100800000dbdfef00000021f90401000000002c00000000010001000002024401003b'; // Then at runtime convert the hex back to a buffer imgBuffer = new Buffer(imgHex, 'hex'); // This buffer can directly sent on the HTTP response stream response.end(imgBuffer, 'binary');
The following gist is a complete integration for serving with the HTTP Server