How to get data out of a Node.js http get request

后端 未结 7 1187
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 23:25

I\'m trying to get my function to return the http get request, however, whatever I do it seems to get lost in the ?scope?. I\'m quit new to Node.js so any help would be appr

相关标签:
7条回答
  • 2020-11-28 23:33

    Of course your logs return undefined : you log before the request is done. The problem isn't scope but asynchronicity.

    http.request is asynchronous, that's why it takes a callback as parameter. Do what you have to do in the callback (the one you pass to response.end):

    callback = function(response) {
    
      response.on('data', function (chunk) {
        str += chunk;
      });
    
      response.on('end', function () {
        console.log(req.data);
        console.log(str);
        // your code here if you want to use the results !
      });
    }
    
    var req = http.request(options, callback).end();
    
    0 讨论(0)
  • 2020-11-28 23:36

    Simple Working Example of Http request using node.

    const http = require('https')
    
    httprequest().then((data) => {
            const response = {
                statusCode: 200,
                body: JSON.stringify(data),
            };
        return response;
    });
    function httprequest() {
         return new Promise((resolve, reject) => {
            const options = {
                host: 'jsonplaceholder.typicode.com',
                path: '/todos',
                port: 443,
                method: 'GET'
            };
            const req = http.request(options, (res) => {
              if (res.statusCode < 200 || res.statusCode >= 300) {
                    return reject(new Error('statusCode=' + res.statusCode));
                }
                var body = [];
                res.on('data', function(chunk) {
                    body.push(chunk);
                });
                res.on('end', function() {
                    try {
                        body = JSON.parse(Buffer.concat(body).toString());
                    } catch(e) {
                        reject(e);
                    }
                    resolve(body);
                });
            });
            req.on('error', (e) => {
              reject(e.message);
            });
            // send the request
           req.end();
        });
    }
    
    0 讨论(0)
  • 2020-11-28 23:38

    from learnyounode:

    var http = require('http')  
    
    http.get(options, function (response) {  
      response.setEncoding('utf8')  
      response.on('data', console.log)  
      response.on('error', console.error)  
    })
    

    'options' is the host/path variable

    0 讨论(0)
  • 2020-11-28 23:38

    from learnyounode:

    var http = require('http')
    var bl = require('bl')
    
    http.get(process.argv[2], function (response) {
        response.pipe(bl(function (err, data) {
            if (err)
                return console.error(err)
            data = data.toString()
            console.log(data)
        }))
    })
    
    0 讨论(0)
  • 2020-11-28 23:49

    I think it's too late to answer this question but I faced the same problem recently my use case was to call the paginated JSON API and get all the data from each pagination and append it to a single array.

    const https = require('https');
    const apiUrl = "https://example.com/api/movies/search/?Title=";
    let finaldata = [];
    let someCallBack = function(data){
      finaldata.push(...data);
      console.log(finaldata);
    };
    const getData = function (substr, pageNo=1, someCallBack) {
    
      let actualUrl = apiUrl + `${substr}&page=${pageNo}`;
      let mydata = []
      https.get(actualUrl, (resp) => {
        let data = '';
        resp.on('data', (chunk) => {
            data += chunk;
        });
        resp.on('end', async () => {
            if (JSON.parse(data).total_pages!==null){
              pageNo+=1;
              somCallBack(JSON.parse(data).data);
              await getData(substr, pageNo, someCallBack);
            }
        });
      }).on("error", (err) => {
          console.log("Error: " + err.message);
      });
    }
    
    getData("spiderman", pageNo=1, someCallBack);
    

    Like @ackuser mentioned we can use other module but In my use case I had to use the node https. Hoping this will help others.

    0 讨论(0)
  • 2020-11-28 23:52

    This is my solution, although for sure you can use a lot of modules that give you the object as a promise or similar. Anyway, you were missing another callback

    function getData(callbackData){
      var http = require('http');
      var str = '';
    
      var options = {
            host: 'www.random.org',
            path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
      };
    
      callback = function(response) {
    
            response.on('data', function (chunk) {
                  str += chunk;
            });
    
            response.on('end', function () {
                  console.log(str);
              callbackData(str);
            });
    
            //return str;
      }
    
      var req = http.request(options, callback).end();
    
      // These just return undefined and empty
      console.log(req.data);
      console.log(str);
    }
    

    somewhere else

    getData(function(data){
    // YOUR CODE HERE!!!
    })
    
    0 讨论(0)
提交回复
热议问题