我想使用app.get从另一个域上的API传递数据。我可以将数据写入控制台,但没有任何内容出现在页面上('〜/ restresults')。express.js:如何使用app.get中的http.request返回的值
这是我到目前为止的代码:
app.get('/restresults', function (req, res) {
var theresults;
var http = require('http');
var options = {
port: '80' ,
hostname: 'restsite' ,
path: '/v1/search?format=json&q=%22foobar%22' ,
headers: { 'Authorization': 'Basic abc=='}
} ;
callback = function(res) {
var content;
res.on('data', function (chunk) {
content += chunk;
});
res.on('end', function() {
console.log(content);
theresults = content ;
});
};
http.request(options, callback).end();
res.send(theresults) ;
});
我怎么能在http.request的结果绑定到一个变量并返回它当“restresults /”要求?