Create a gist now

Instantly share code, notes, and snippets.

Node.js + Gzip/Deflate Compression
var zlib = require('zlib');
var compress = function(req, res, result) {
var acceptEncoding = req.headers['accept-encoding'];
if (!acceptEncoding) { acceptEncoding = ''; }
if (acceptEncoding.match(/\bdeflate\b/)) {
zlib.deflate(result, function(err, result) {
if (!err) {
res.writeHead(200, { 'content-encoding': 'deflate' });
res.send(result);
}
});
} else if (acceptEncoding.match(/\bgzip\b/)) {
zlib.gzip(result, function(err, result){
if (!err) {
res.writeHead(200, { 'content-encoding': 'gzip' });
res.send(result);
}
});
} else {
res.writeHead(200, {});
res.send(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment