我想使用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 /」要求?