Monthly Archives: آذر 1392

Serving zipped files in nodejs, how to cache instead of recompress?

Salam (means Hello) :)

I'm developing a static file server that must support data-encoding: gzip, the example in zlib documentation is working fine for me, but I need to save zipped files to disk, to avoid re-compressing for future requests. here is my code, which doesn't work:

http.createServer(function(request, response) {
    var url = my_normalizer(request.url);
    var acceptEncoding = request.headers['accept-encoding'] || '';

            var zipped = fs.createReadStream(url +'.gz');
            var inp = fs.createReadStream(url);
            var out = fs.createWriteStream(url+'.gz');
            var zipped = fs.createReadStream(url +'.gz');
        response.writeHead(200, { 'content-encoding': 'gzip' });

and this is the result in firefox:

Content Encoding Error

The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression.

for what it's worth, a .gz version of the requested file is correctly generated and saved to disk.