I am designing a node js application which will get requests from frontends web applications and serve them data. It also uses some third party APIs that requires some token. Previously the token was stored in a config file. So Previously we had something like this.
config.js
module.exports = {
token: 'xxxxxxxxxxxx'}
CustomModule1.js
const configuration = require('./config');
let client = thirdparty1.createClient({
token: configuration.token});
function BusLogic(param){
client.relevantWork().then(
//return data
)
}
module.exports.BusLogic = BusLogic;
index.js
const customModule = require('./CustomModule1');
module.exports = function (context, req){
customModule1.BusLogic(req.param).then(res => {
//return frontEndData
})
}
Now we are storing token in a secure third party vault which requires another api call so I changed the structure to below.
config.js
let vaultFetch = (param1){
return getfromVault(param1)
}
module.exports = {
token: vaultFetch('tokenData')}
CustomModule1.js
function BusLogic(param,client){
client.relevantWork().then(
//return data
)
}
module.exports.BusLogic = BusLogic;
index.js
const customModule = require('./CustomModule1');
module.exports = function (context, req){
configuration.then(x => {
thirdparty1.createClient({
token: configuration.token}).then(client => {
customModule1.BusLogic(req.param,client).then(res => {
//return frontEndData
})
})
})
}
But as it so happens in case of multiple concurrent requests the api is taking a long time to fetch data. It may be caused because client is being created for every request. Is there a way to create client one time somehow by using a promise?