Nodejs关于gzip/deflate压缩


http://homeway.me



0x01.关于

写http时候,在接收http请求时候,出现乱码,后来发现是gzip没有解压。

关于gzip/deflate压缩,有放入管道压缩,和非管道压缩方法。


0x02.非管道压缩

代码如下:

#! /usr/local/bin/node

var http = require('http'),
    querystring = require('querystring'),
    zlib = require('zlib');
var args = {
    //参数以及备用数据
    contents : querystring.stringify({  
        //发包的信息
        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);
});

0x02.管道压缩

Node中的I/O是异步的,因此对磁盘和网络的读写需要通过回调函数来读取数据。

当内存中无法一次装下需要处理的数据时,或者一边读取一边处理更加高效时,我们就需要用到数据流。

NodeJS中通过各种Stream来提供对数据流的操作。

官网提供了管道方法:

// 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;
    }
});




本文出自 夏日小草,转载请注明出处: http://homeway.me/2015/03/03/nodejs-gzip-deflate-compression
by 小草

2015-03-03 14:17:20

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值