Reputation: 41
I would like to know how it is possible to centralize on a single api the data of several api
I would like to have the result of the four res.data at the address /coingeckotest
Currently I can see in the terminal the result I want to have but on the page /coingeckotest I have a return of "null" value
Thanks in advance for your answers
this is my script
async function datauniswap() {
let datauniswap = await axios
.get('https://api.coingecko.com/api/v3/exchanges/uniswap/tickers')
.then((res) => {
console.log(res.data)
})
return datauniswap
}
async function datasushiswap() {
let datasushiswap = await axios
.get('https://api.coingecko.com/api/v3/exchanges/sushiswap/tickers')
.then((res) => {
console.log(res.data)
})
return datasushiswap
}
async function datacurvefinance() {
let datacurvefinance = await axios
.get('https://api.coingecko.com/api/v3/exchanges/curve/tickers')
.then((res) => {
console.log(res.data)
})
return datacurvefinance
}
async function dataquickswap() {
let dataquickswap = await axios
.get('https://api.coingecko.com/api/v3/exchanges/quickswap/tickers')
.then((res) => {
console.log(res.data)
})
return dataquickswap
}
server.get('/coingeckotest', async (req, res) => {
exchangeone = await datauniswap();
exchangetwo = await datasushiswap();
exchangethree = await datacurvefinance();
exchangequattro = await dataquickswap();
cacheTime = Date.now();
res.json[exchangeone, exchangetwo, exchangethree, exchangequattro]
})
Upvotes: 1
Views: 45
Reputation: 1209
Solution:
async function datauniswap() {
// can be configured via axios config baseUrl:
// https://axios-http.com/docs/config_defaults
let url = 'https://api.coingecko.com/api/v3/exchanges/uniswap/tickers'
// TODO: handle errors via try/catch
let response = await axios.get(url);
// TODO: handle when data is undefined
return response?.data
}
res.json[exchangeone, exchangetwo, exchangethree, exchangequattro]
If you are using express.js for the server- .json
- is a function, you can try this:
Solution:
let data = [exchangeone, exchangetwo, exchangethree, exchangequattro]
res.json(data)
Upvotes: 1