Ho trovato che il modulo di richiesta in js non è in grado di gestire correttamente la risposta http in formato gzip o inflate.Come utilizzare la richiesta o il modulo http per leggere la pagina gzip in una stringa
ad esempio:
request({url:'some url'}, function (error, response, body) {
//if the content-encoding is gzip, the body param here contains binaries other than readable string. And even worse after you convert the body to buffer, u even can not gunzip it.
}
quindi voglio utilizzare il codice di esempio nella documentazione ufficiale.
var request = http.get({ host: 'izs.me',
path: '/',
port: 80,
headers: { 'accept-encoding': 'gzip,deflate' } });
request.on('response', function(response) {
var output = fs.createWriteStream('izs.me_index.html');
switch (response.headers['content-encoding']) {
// or, just use zlib.createUnzip() to handle both cases
case 'gzip':
response.pipe(zlib.createGunzip()).pipe(output);
break;
case 'deflate':
response.pipe(zlib.createInflate()).pipe(output);
break;
default:
response.pipe(output);
break;
}
});
Il problema è che il codice sta scrivendo la pagina web in un file, spero che possa scrivere la pagina in una stringa, in modo che possa elaborare la pagina. Non ho trovato nessuna classe come "StringStream".
Se qualcuno ha qualche idea su questo, sarà fantastico.
Se createUnzip in grado di gestire sia gzip e gonfiare, perché preoccuparsi di scrivere il codice in più? È più lento? – CMCDragonkai