简体   繁体   中英

Not even returning an error? node.js request

I have a script that GETS with http.request a website. I have placed console.log()'s everywhere, yet can't even get it to 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 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() . 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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