Nodejs關(guān)于gzip/deflate壓縮詳解
0x01.關(guān)于
寫http時(shí)候,在接收http請(qǐng)求時(shí)候,出現(xiàn)亂碼,后來(lái)發(fā)現(xiàn)是gzip沒(méi)有解壓。
關(guān)于gzip/deflate壓縮,有放入管道壓縮,和非管道壓縮方法。
0x02.管道壓縮
Node中的I/O是異步的,因此對(duì)磁盤和網(wǎng)絡(luò)的讀寫需要通過(guò)回調(diào)函數(shù)來(lái)讀取數(shù)據(jù)。
當(dāng)內(nèi)存中無(wú)法一次裝下需要處理的數(shù)據(jù)時(shí),或者一邊讀取一邊處理更加高效時(shí),我們就需要用到數(shù)據(jù)流。
NodeJS中通過(guò)各種Stream來(lái)提供對(duì)數(shù)據(jù)流的操作。
官網(wǎng)提供了管道方法:
// client request example
var zlib = require('zlib');
var http = require('http');
var fs = require('fs');
var request = http.get({ host: 'homeway.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;
}
});
0x03.非管道壓縮
代碼如下:
#! /usr/local/bin/node
var http = require('http'),
querystring = require('querystring'),
zlib = require('zlib');
var args = {
//參數(shù)以及備用數(shù)據(jù)
contents : querystring.stringify({
//發(fā)包的信息
name:'homeway.me',
}),
};
var options = {
hostname: 'homeway.me',
port: 80,
path: '/',
method: 'GET',
headers: {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Content-Length': args.contents.length,
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.11 Safari/537.36',
'Accept-Encoding':'gzip, deflate',
},
};
var get = function ( options, args, callback ){
var req = http.request(options, function (res) {
var chunks =[], data, encoding = res.headers['content-encoding'];
// 非gzip/deflate要轉(zhuǎn)成utf-8格式
if( encoding === 'undefined'){
res.setEncoding('utf-8');
}
res.on('data', function (chunk){
chunks.push(chunk);
});
res.on('end', function (){
var buffer = Buffer.concat(chunks);
if (encoding == 'gzip') {
zlib.gunzip(buffer, function (err, decoded) {
data = decoded.toString();
callback( err, args, res.headers, data);
});
} else if (encoding == 'deflate') {
zlib.inflate(buffer, function (err, decoded) {
data = decoded.toString();
callback( err, args, res.headers, data);
});
} else {
data = buffer.toString();
callback( null, args, res.headers, data);
}
});
});
req.write( args.contents );
req.end();
};
get( options, args, function (err, args, headers, data){
console.log('==>header \n', headers);
console.log('==data \n', data);
});
以上就是Nodejs關(guān)于gzip/deflate壓縮的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
用NodeJS實(shí)現(xiàn)批量查詢地理位置的經(jīng)緯度接口
最近要實(shí)現(xiàn)一個(gè)顯示各個(gè)城市信息的功能,后臺(tái)一看包含一堆城市的excel,發(fā)現(xiàn)不僅有每個(gè)省的直轄市,還有二三線等的城市,數(shù)量還不少,一個(gè)個(gè)去查還挺浪費(fèi)時(shí)間的,那為什么不寫個(gè)腳本去實(shí)現(xiàn)批量查詢呢。2016-08-08Node.js Windows Binary二進(jìn)制文件安裝方法
這篇文章主要介紹了Node.js Windows Binary二進(jìn)制文件安裝,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05node異步使用await和不用await的區(qū)別實(shí)例分析
這篇文章主要介紹了node異步使用await和不用await的區(qū)別,結(jié)合實(shí)例形式分析了node.js異步使用await和不用await的實(shí)例中,同步與異步執(zhí)行的區(qū)別,需要的朋友可以參考下2023-06-06node.js中使用ejs渲染數(shù)據(jù)的代碼實(shí)現(xiàn)
這篇文章主要介紹了node.js中使用ejs渲染數(shù)據(jù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11nodejs事件的監(jiān)聽(tīng)與觸發(fā)的理解分析
這篇文章主要介紹了nodejs事件的監(jiān)聽(tīng)與觸發(fā)的理解分析,以實(shí)例形式對(duì)比分析了nodejs與jQuery關(guān)于事件監(jiān)聽(tīng)的實(shí)用技巧,有助于加深對(duì)nodejs的理解,需要的朋友可以參考下2015-02-02node.js中的console.timeEnd方法使用說(shuō)明
這篇文章主要介紹了node.js中的console.timeEnd方法使用說(shuō)明,本文介紹了console.timeEnd的方法說(shuō)明、語(yǔ)法、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12