Javascript 如何从Node.js中的REST API导出GET响应?
下面是Javascript 如何从Node.js中的REST API导出GET响应?,javascript,node.js,Javascript,Node.js,下面是getresponse.js的代码 var https = require('https'); function httpGet(callback){ var body = ""; var issueDesc = []; var options = { hostname:'<hostname>' port:443, path:'/rest/api/2/search?j
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;
}))
我想访问app.js
中的issueDesc
,但由于回调不是函数,所以出现了错误
是否必须通过module.exports
?getresponse.js文件将issueDesc
作为全局变量发送
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);
})
});
});
}
}
您将在“then”块中得到响应。getresponse.js文件
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);
})
});
});
}
}
您将在“then”块中得到响应。module.exports.httpGet=httpGet();应为module.exports.httpGet=httpGet@NiralMunjariya谢谢我现在编辑为了更好的可读性,你可以使用Promise而不是回调函数。@NiralMunjariya好的,但是我如何解决我的问题呢?module.exports.httpGet=httpGet();应为module.exports.httpGet=httpGet@NiralMunjariya谢谢我现在编辑为了更好的可读性,你可以使用Promise而不是回调函数。@NiralMunjariya好的,但是我如何在这里解决我的问题?谢谢!没有太多的“承诺”概念。它起作用了!。谢谢没有太多的“承诺”概念。它起作用了!。
var getissue = require('./getresponse.js');
getissue.httpGet()
.then(response => {
console.log('your response', response);
}).catch(err => {
console.error('error', err);
});