Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

TypeError: object is not a function问题(涉及到https+gzip解压)

$
0
0

文件结构是 script.js + client 目前是写一份client文件,form的数据通过脚本来实现https提交post,这里出现抛错信息,https在返回时判断是不是gzip 使用了zlib进行解压,但出现了TypeError: object is not a function问题已经改过命名了,但还有问题。 node.js小菜猫一只,求高手和热心者帮助。

抛错信息如下:

var req = hreq.post(this.options,function (res) {
                   ^
TypeError: hreq.post is not a function
    at Client.httpRequest (D:\QA\performance-framework\node_modules\xxxx\xxxx-client\lib\client.js:69:20)
    at Client.fn (D:\QA\performance-framework\app\script\optest\script.js:65:12)
    at Timeout.breath [as _onTimeout] (D:\QA\performance-framework\node_modules\xxxx\xxxx-client\lib\client.js:34:18)
    at ontimeout (timers.js:386:14)
    at tryOnTimeout (timers.js:250:5)
    at Timer.listOnTimeout (timers.js:214:5)

抛错的client.js:69:20=> var req = hreq.post(this.options,function (res) //this.options不知道是否有错,如果不这样写找不到options 影响在script.js文件内的

op.httpRequest('/checkin/test/xxx',       //client负责完整url和处理,client客户端配置后,通过Client.prototype.httpRequest来提交boby.
            {
               ......
            }

client.js引用模块如下

var zlib = require('zlib');
var request = require('request');
var hreq =require('https').request;
var querystring = require('querystring');
Client.prototype.init = function (params, cb) 

问题来源具体代码如下:

Client.prototype.init = function (params, cb) {        //prototype init
    this.port = params.port;
    this.host = params.host;

    this.options = {
        agent: false,
        headers: {
            'Accept-Encoding': 'gzip',
            'Content-Type':'application/x-www-form-urlencoded'          
        }
    };
    setImmediate(cb);
}

Client.prototype.httpRequest = function (path, param, callback) {
    this.options.url = 'https://' + this.host + ':' + this.port + path;
    this.options.body = querystring.stringify(param);
    console.log('options <%s>', JSON.stringify(this.options)); //querystring->JSON请求查看options

    var req = hreq.post(this.options,function (res) {   //this.options不知道是否有错,如果不这样写找不到options

            res.on('response', function(){        //res事件
            var chunks = [];

            res.on('data', function (chunk) {      //用于拼接
                chunks.push(chunk);
            });

            res.on('end', function() {
                var message = {};
                message.code = res.statusCode;

                var buffer = Buffer.concat(chunks);
                var encoding = res.headers['content-encoding'];    //下面用于判断格式
                console.log(encoding);

                switch (encoding) {
                    case 'gzip':
                        zlib.gunzip(buffer, function (err, decoded) {      //zlib.gunzip
                            message.body = decoded && decoded.toString()
                            callback(message);
                        });

                        break;
                    case 'deflate':
                        zlib.inflate(buffer, function (err, decoded) {
                            message.body = decoded && decoded.toString()
                            callback(message);
                        });

                        break;
                    default:
                        message.body = buffer.toString()
                        callback(message);
                }
            });
        });
    });
    req.on('error', function (err) {
        console.error(err);
        callback(err);
    });
};

Viewing all articles
Browse latest Browse all 14821

Trending Articles