Nodejs關于gzip/deflate壓縮詳解
0x01.關于
寫http時候,在接收http請求時候,出現(xiàn)亂碼,后來發(fā)現(xiàn)是gzip沒有解壓。
關于gzip/deflate壓縮,有放入管道壓縮,和非管道壓縮方法。
0x02.管道壓縮
Node中的I/O是異步的,因此對磁盤和網(wǎng)絡的讀寫需要通過回調函數(shù)來讀取數(shù)據(jù)。
當內存中無法一次裝下需要處理的數(shù)據(jù)時,或者一邊讀取一邊處理更加高效時,我們就需要用到數(shù)據(jù)流。
NodeJS中通過各種Stream來提供對數(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要轉成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關于gzip/deflate壓縮的全部內容了,希望大家能夠喜歡。
相關文章
詳解node Async/Await 更好的異步編程解決方案
這篇文章主要介紹了詳解Async/Await 更好的異步編程解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
node.js使用stream模塊實現(xiàn)自定義流示例
這篇文章主要介紹了node.js使用stream模塊實現(xiàn)自定義流,結合實例形式詳細分析了node.js基于stream模塊實現(xiàn)自定義的可讀流、可寫流、可讀寫流等相關操作技巧,需要的朋友可以參考下2020-02-02
用純Node.JS彈出Windows系統(tǒng)消息提示框實例(MessageBox)
這篇文章主要介紹了用純Node.JS彈出Windows系統(tǒng)消息提示框實例(MessageBox),非常具有實用價值,需要的朋友可以參考下2017-05-05
nodejs發(fā)布靜態(tài)https服務器步驟指南
這篇文章主要為大家介紹了nodejs發(fā)布靜態(tài)https服務器的步驟指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-02-02
詳解如何在Node.js的httpServer中接收前端發(fā)送的arraybuffer數(shù)據(jù)
這篇文章主要介紹了詳解如何在Node.js的httpServer中接收前端發(fā)送的arraybuffer數(shù)據(jù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
Node.js利用Net模塊實現(xiàn)多人命令行聊天室的方法
Node.js Net 模塊提供了一些用于底層的網(wǎng)絡通信的小工具,包含了創(chuàng)建服務器/客戶端的方法,下面這篇文章主要給大家介紹了Node.js利用Net模塊實現(xiàn)命令行多人聊天室的方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12

