带有 zlib 库的 Node.js 请求

标签 node.js http request zlib

我正在尝试找出一种方法来制作 this库使用 gzip 和 deflate。

看了this问题,尝试了这些建议,但由于某种原因它仍然不起作用。我对 node.js 比较陌生,有人可以看看我的代码并告诉我我做错了什么吗?

var request = require("request"),
zlib = require( 'zlib' );
var req = request( {
    uri: "http://google.com",
    headers: {
        'Accept-Encoding': 'gzip, deflate',
        'user-agent': 'Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/13.0.1',
        },
    }, 
function( error, res, body ) {
    var output;
    switch ( res.headers[ 'content-encoding' ] ) {
        case 'gzip':
            var gzip = zlib.createGunzip();
            res.pipe(gzip);
            output = gzip;
        break;
        case 'deflate':
            var gzip = zlib.createInflate();
            res.pipe(gzip);
            output = gzip;
        break;
        default:
            res.setEncoding( 'utf8' );
            output = res;
        break;
    }
    //console.log( body ) // outputs encoded html
    output.on('data', function ( data ) {
        console.log( data ); // never happens
    });
    output.on('end', function() {
        console.log( data ); // never happens
    });
});

最佳答案

解决者:

  1. 将 node.js 更新到 0.10
  2. 使用描述的第一个解决方案 here

关于带有 zlib 库的 Node.js 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16378377/

相关文章:

android - facebook 批处理请求返回空对象

javascript - 如何在请求中操作 res.locals 对象?

javascript - 为什么lodash中的_.defaults()需要大量CPU时间?

javascript - d3.js 具有相同父 Node 的树 Node

http - jmeter 在多个服务器上运行相同的 HTTP 请求

node.js - 由于每秒上限而限制和排队 API 请求

javascript - 在公共(public) JS 文件中使用 Node.js

http - URL中的参数和<form method ="get">有什么区别吗?

javascript - 在建立TLS连接时,如何传递其他信息?

php - PHP 中的异步 HTTP 请求