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:
- 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?
- 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?