I am currently trying to implement a BFF (backend for front end architecture).
Using request-promise
library I can successfully hit the other microservice but not able to return the result as a response from BFF microservice.
Each time it is returning this result Promise { pending }
pending state, could somebody please help me out on this?
My main issue is to know how to receive data into BFF microservice from the other microservice that we are hitting and returning the result from microservice which is hitting other one.
Or if somebody could help me to let know how to access the result from inside .then
of any promise?
The flow is like this:
client(ios/android)===(sends request)==>BFF Microservice==>BP microservice
(BFF Microservice handles the request and returns the response on the basis of result received from other microservice)
Microservice code which is calling another microservice:
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
code:
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)
})
}