Reputation: 71
I need to call 2 different endpoint api and fetch data from both, currently I'm only able to call each one individually.
Here is my code so far:
app.get('/', function(req, res) {
const url = 'https://allsportsapi.com/api/football/?met=Fixtures&APIkey=myapikey&from=2020-07-23&to=2020-07-24&leagueId=262';
https.get(url, function (response) {
let body = '';
response.on('data', function(chunk){
body += chunk;
});
response.on('end', function(){
const apiResponse = JSON.parse(body);
const blob = apiResponse;
const stan = apiResponse.result[0].event_home_team;
const away_1 = apiResponse.result[0].event_away_team;
const stadium = apiResponse.result[0].event_stadium;
const prova = apiResponse.result[0].home_team_key;
console.log('got a response: ');
// console.log(blob);
res.render('test', {team: stan, fuori:away_1, stadio: stadium});
});
}).on('error', function(e){
console.log("Got an error: ", e);
});
});
app.listen(3000, function() {
console.log('Server started on port 3000');
});
I need to call another api which is: https://gnews.io/api/v3/topics/sports?&token=
Thanks in advance.
Upvotes: 1
Views: 50
Reputation: 1538
Use axios which is much better for making http requests.
Also if these requests have no dependency on each other then you can use axios.all
as shown below
axios.all([
axios.get('https://allsportsapi.com/api/football/?met=Fixtures&APIkey=myapikey&from=2020-07-23&to=2020-07-24&leagueId=262'),
axios.get('https://gnews.io/api/v3/topics/sports?&token=')
])
.then(axios.spread((res1, res2) => {
console.log(res1.data);
console.log(res2.data);
}));
Upvotes: 2