Options to be used with zlib module in Node.js to compress javascript files to be used with gzip encoding in browser

380 Views Asked by At

I am using Node.js as a HTTP server. I do not use the Express module. I want to compress a javascript file, send the resulting string after setting headers in the middleware, as in:

    zlib = require ("zlib");
    res.setHeader('Content-Encoding', 'gzip');
    res.setHeader('Content-Type', 'text/javascript');
    
    //Compress the javascript
    zlib.gzip(js_string, (err, buffer) => { //js_string is a string not buffer
       if (err) {
            //processError...
       }
       else {
           res.end(buffer.toString('hex'));
       }
    });

The questions that I have are:

  1. zlib.gzip has a second parameter called options. What options, if any, do I need to specify if the gzip is created to be unzipped by the browser?
  2. What kind of buffer-to-string encoding is to be done, 'hex', 'base64',... again if the usage is for creating a compressed content to be uncompressed by the browser? Should I be using "Content-Transfer-Encoding" header to indicate to the browser the encoding type?
0

There are 0 best solutions below