Skip to content

HTTP Compression? #671

Answered by Wyzix33
digitalml asked this question in Q&A
Dec 22, 2021 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

You can use zlib, this is the way i used it:

const zlib = require('zlib');

and in your code you should check if the client accept encoding

const acceptEncoding = req.getHeader('accept-encoding');
let readStream = toArrayBuffer(fs.readFileSync(fileName));
 if (/\bgzip\b/.test(acceptEncoding)) {
     res.writeHeader('Content-Encoding', 'gzip');
     readStream = zlib.gzipSync(readStream);
    } else if (/\bdeflate\b/.test(acceptEncoding)) {
     res.writeHeader('Content-Encoding', 'deflate');
     readStream = zlib.deflateSync(readStream);
    }
    // else just original uncompressed 
    res.end(readStream);

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@digitalml
Comment options

Answer selected by digitalml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants