0
複数のapiから集約応答を投げるREST APIを設計しようとしています。続きNodejを使用して複数のapiから結果を取得するREST APIを設計する
はNodeJSコードで実行しようとしています -
Pseudo Code start
//endpoint to be called from a browser/REST client
router.get('/api/v1/getItems', (req, response, next) => {
var result = {} // hold the aggregated response from multiple apis
//internally fire another endpoint & add the response over to the var result
http.get(endpoint 1, function(resp){
add response to result})
http.get(endpoint 2, function(resp){
add response to result
})
return response.json(result);
}
Pseudo Code end
// endpoint to be called from the browser or REST Client.
router.get('/api/v1/getItems', (req, response, next) => {
var results = {};
// Nested Endpoint 1
var optionsgetmsg = {
host : 'host.domain.com', // tthe domain name
port : 9043,
path : '/services/itemdata', // the rest of the url
method : 'GET' // do GET
};
//child endpoint
var reqGet = http.request(optionsgetmsg, function(res) {
res.on('data', function(d) {
console.log("d "+ d); // child response
results.itemdata = d;
return response.send(results);
//process.stdout.write(d);
});
res.on('end', function(d){
})
});
reqGet.end();
reqGet.on('error', function(e) {
console.error(e);
});
});
上記の場合の結果を出力する「D」でなければなりません。出力 'd'は、子エンドポイントからの応答です。
実際の結果は空のオブジェクトです。 {}
シリアライズされた結果を返す、よりフレンドリーなhttpクライアントを使用するか、ストリームで読み上げることをお勧めします。https://nodejs.org/api/stream.html#stream_api_for_stream_consumers – ginman
ここでは何を求めているのかは不明です。あなたの問題/質問を黙認してください。 – kentor