現在、BFF (フロント エンド アーキテクチャのバックエンド) を実装しようとしています。
ライブラリを使用request-promise
すると、他のマイクロサービスを正常にヒットできますが、BFF マイクロサービスからの応答として結果を返すことができません。
この結果保留状態が返されるたびにPromise { pending }
、誰かが私を助けてくれませんか?
私の主な問題は、ヒットしている他のマイクロサービスから BFF マイクロサービスにデータを受信し、他のマイクロサービスにヒットしているマイクロサービスから結果を返す方法を知ることです。
.then
または、約束の内部から結果にアクセスする方法を誰かが教えてくれるとしたら?
流れは次のようになります。
client(ios/android)===(sends request)==>BFF Microservice==>BP microservice
(BFF マイクロサービスはリクエストを処理し、他のマイクロサービスから受け取った結果に基づいてレスポンスを返します)
別のマイクロサービスを呼び出しているマイクロサービス コード:
import yagmodel from '../../lib/models/yag-model'
import {errorsList} from '../../lib/errors/errorsList'
import request from 'request-promise'
import config from 'config'
//template below to call the REST APIs of other microservices.
export async function getAllBP (req,res) {
let yagresponse// this varaible is defined to get data from inside(rs.then )
const username= req.swagger.params.username.value
const authheader= req.swagger.params.Authorization.value
console.log("Authorization:"+authheader)
let rs= await yagmodel.bp(username,authheader)
console.log(rs)
rs.then((response)=>{
// console.log(response.body)
yagresponse=response.body
//console.log(rsp)
}).catch((err)=>{
console.log(err)
console.log('errorstatuscode:'+err.statusCode)
})
res.status(200).send(yagresponse)
}
yag-model.js
コード:
import {errorsList} from '../../lib/errors/errorsList'
import request from 'request-promise'
module.exports.bp = async function getBP(username,authheader){
const options={
uri: `http://localhost:4000/Health/BP/`+username,
json: true,
resolveWithFullResponse: true,
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json; charset=utf-8',
'Authorization':authheader
},
method: 'GET'
}
return request(options).then ((response)=>{
return response.body
}).catch((err)=>{
console.log(err)
console.log('errorstatuscode:'+err.statusCode)
})
}