简体   繁体   中英

How to export my GET response from REST API in Node.js?

Below is code for getresponse.js

var https = require('https');  

    function httpGet(callback){
        var body = "";
        var issueDesc = [];

        var options = {
        hostname:'<hostname>'
        port:443,
        path:'/rest/api/2/search?jql=project=X',
        method:'GET',
        headers: {
                'Authorization':'Basic '+ new Buffer("xxxxx"+':'+"xxxxxx").toString('base64'),
                'Content-Type':'application/json',
                }
        };


    https.get(options, function(res, callback) {


            res.on('data', function(data) {
               body += data;
            });
        res.on('end', function(callback) {
              Response = JSON.parse(body);        

                 var i=Response.total;
                          for(j=0;j<i;j++){

                                     issueDesc.push(Response.issues[j].key +": "+Response.issues[j].fields.summary +"\n");               
                             }   

                         issueDesc = issueDesc.join("");
                     callback(issueDesc);
                        });
          });   
    };

    module.exports.httpGet = httpGet;

Below is my code for app.js (controller file)

var getissue = require('./getresponse.js');
getissue.httpGet(function(response) {
console.log(response);
// ....

});

I want to access the issueDesc in app.js but i am getting error as callback is not a function .

Do i have to send the issueDesc as global variable through module.exports ?

getresponse.js file

module.exports = {
  httpGet: () => {
    var https = require('https');
    return new Promise((resolve, reject) => {
      var body = "";
      var issueDesc = [];
      var options = {
        hostname: '<hostname>'
        port: 443,
        path: '/rest/api/2/search?jql=project=X',
        method: 'GET',
        headers: {
          'Authorization': 'Basic ' + new Buffer("xxxxx" + ':' + "xxxxxx").toString('base64'),
          'Content-Type': 'application/json',
        }
      };
      https.get(options, function (res) {    
        res.on('data', function (data) {
          body += data;
        });
        res.on('end', function () {
          var response = JSON.parse(body);
          var i = response.total;
          for (j = 0; j < i; j++) {
            issueDesc.push(response.issues[j].key + ": " + response.issues[j].fields.summary + "\n");
          }
          issueDesc = issueDesc.join("");
          resolve(issueDesc);
        });
        res.on('error', (err) => {
          reject(err);
        })
      });
    });
  }
}

app.js

var getissue = require('./getresponse.js');
getissue.httpGet()
  .then(response => {
    console.log('your response', response);
  }).catch(err => {
    console.error('error', err);
  });

You'll get the response in the 'then' block.

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