vendredi 27 mars 2015

How to cache the response and send it at once?

The following is working code that sends proxy response successfully



proxy_request.addListener('response', function(proxy_response) {
response.writeHead(proxy_response.statusCode, proxy_response.headers);
proxy_response.addListener('data', function(chunk) {
response.write(buffer, 'binary');
});

proxy_response.addListener('end', function() {
response.end();
});
}


The following code is modification of the above to cache the data until all the data is received and then to send all at once. It does not work despite recalculating the headers.



proxy_request.addListener('response', function(proxy_response) {
var buffer = "";
proxy_response.addListener('data', function(chunk) {
buffer += chunk;
});
proxy_response.addListener('end', function() {
proxy_response.headers['content-length'] = Buffer.byteLength(buffer, proxy_response.headers['content-encoding']);
response.writeHead(proxy_response.statusCode, proxy_response.headers);
response.write(buffer, 'binary');
response.end();
});
}


The firefox displays the following error message



Content Encoding Error


The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression.


Please contact the website owners to inform them of this problem.



The idea is to store the request in mongodb and later to fetch from the db and serve it to the client if available.


Aucun commentaire:

Enregistrer un commentaire