Running a gzip operation on every request is quite expensive.
It would be much more efficient to cache the compressed buffer.
But "
Compression is a simple, effective way to save bandwidth and speed up your site."
How it works.
In this case we are sending different encoding request to server .
Server compress data according to Encoding .
And server will response compress data . [
So that file will download fast as size reduced]
Then client responsibility to decompress data . [
As its decompress quite expensive . Still all modern browser supports this ]
Example Encoding Client :: EncodingClient.js
// client request example
var zlib = require('zlib');
var http = require('http');
var fs = require('fs');
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var acceptEncoding = null;
rl.question("\nWhat type accept-encoding you want to set for this request gzip/deflate/no? ", function (answer) {
// TODO: Log the answer in a database
if (answer === "gzip" || answer === "deflate") {
console.log("\nRequest with encoding ::", answer);
acceptEncoding = answer;
}else{
acceptEncoding=null;
console.log("\nRequest with no encoding");
}
rl.close();
var request = http.get({
host: 'localhost',
path: '/',
port: 8585,
headers: {
'accept-encoding': acceptEncoding
}
});
request.on('response', function (response) {
switch (response.headers['content-encoding']) {
// or, just use zlib.createUnzip() to handle both cases
case 'gzip':
var length = 0;
var gzippipe = response.pipe(zlib.createGunzip(), {
end: false
});
response.on('data', function (chunk) {
length = length + chunk.length;
});
response.on('end', function () {
console.log("gzip Encoding Response Data Length ::" + length);
});
var output = fs.createWriteStream('Download_gZip_Ticket.pdf');
gzippipe.pipe(output);
break;
case 'deflate':
var length = 0;
var deflatepipe = response.pipe(zlib.createInflate(), {
end: false
});
response.on('data', function (chunk) {
length = length + chunk.length;
});
response.on('end', function () {
console.log("deflate Encoding Response Data Length ::" + length);
});
var output = fs.createWriteStream('Download_deflate_Ticket.pdf');
deflatepipe.pipe(output);
break;
default:
var length = 0;
var output = fs.createWriteStream('Download_normal_Ticket.pdf');
response.pipe(output, {
end: false
});
response.on('data', function (chunk) {
length = length + chunk.length;
});
response.on('end', function () {
console.log("deflate Encoding Response Data Length ::" + length);
});
break;
}
});
});
Example Encoding Server :: EncodingServer.js
var zlib = require('zlib');
var http = require('http');
var fs = require('fs');
console.log("Startd Server");
http.createServer(function (request, response) {
console.log("Request Received");
var raw = fs.createReadStream('Ticket.pdf');
var acceptEncoding = request.headers['accept-encoding'];
if (!acceptEncoding) {
acceptEncoding = '';
}
console.log("Request acceptEncoding=" + acceptEncoding);
if (acceptEncoding.match(/\bdeflate\b/)) {
response.writeHead(200, {
'content-encoding': 'deflate'
});
raw.pipe(zlib.createDeflate()).pipe(response);
} else if (acceptEncoding.match(/\bgzip\b/)) {
response.writeHead(200, {
'content-encoding': 'gzip'
});
raw.pipe(zlib.createGzip()).pipe(response);
} else {
var length = 0;
response.writeHead(200, {});
raw.pipe(response);
}
}).listen(8585);
How to Execute: