简体   繁体   English

甚至没有返回错误? node.js请求

[英]Not even returning an error? node.js request

I have a script that GETS with http.request a website. 我有一个脚本,GETS与http.request一个网站。 I have placed console.log()'s everywhere, yet can't even get it to log. 我已将console.log()放在任何地方,但甚至无法将其记录下来。 Here's my code: 这是我的代码:

var dg = {
            hostname: 'www.roblox.com',
            path: '/studio/plugins/info?assetId=12313013',
            headers: {
                'Accept-Encoding': 'gzip'
            }
        };

                var omagawsh = http.request(dg, function(rspn) {
            var strn = []
            var gunzip = zlib.createGunzip();
            rspn.pipe(gunzip)
            gunzip.on('error', function(e) {

            })
            gunzip.on('data', function(chunk) {
                strn.push(chunk)
            });
            gunzip.on('end', function() {
              });
        omagawsh.on('error', function(e) {

        })
        omgawsh.end();
        })

I am using 我在用

var zlib = require('zlib') var zlib = require('zlib')

var http = require('http') var http = require('http')

at the top. 在顶部。

Any help is much appreciated, thanks 非常感谢任何帮助,谢谢

You never actually write the request which you would do with omgawsh.end() . 你实际上从未写过使用omgawsh.end()的请求。 The request will not actually be sent until this is done. 在完成此操作之前,实际上不会发送请求。

var omagawsh = http.request(dg, function(rspn) {
    /* snip */  
    omgawsh.end();
})

Since the request is not done, the response callback is not reached. 由于未完成请求,因此未达到响应回调。 Your calls to omgawsh should be outside the callback. 您对omgawsh调用应该在回调之外

var omagawsh = http.request(dg, cb);
omagawsh.on("error", handleError)
omagawsh.end()

See the documentation for http.request : https://nodejs.org/api/http.html#http_http_request_options_callback 请参阅http.request文档: httpshttp.request

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM