Created
April 2, 2012 10:38
-
-
Save lyhcode/2282472 to your computer and use it in GitHub Desktop.
Node.js + Gzip/Deflate Compression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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