Browse Source

:sparkless: call 3 API

main
lamya-rey 5 years ago
parent
commit
b70116418c
  1. 51
      index.js

51
index.js

@ -4,11 +4,54 @@ import axios from 'axios';
const app = fastify({ logger: true });
app.get('/', async (req, res) => {
//call CatFacts API
async function getCatFacts() {
try {
const response = await axios({
url: 'https://cat-fact.herokuapp.com/facts/random?amount=3',
method: 'GET',
});
return response.data.map(cat => cat.text)
} catch (err) {
return null;
}
}
//call Fox API
async function getFoxImage() {
try {
const response = await axios({
url: 'https://randomfox.ca/floof/',
method: 'GET',
});
return response.data.image;
} catch (err) {
return null;
}
}
//call PublicHolidays API
async function getDayOff(countryCode) {
try {
const response = await axios({
url: `https://date.nager.at/api/v2/PublicHolidays/2021/${countryCode}`,
method: 'GET',
});
return response.data;
} catch (err) {
return null;
}
}
app.post('/', async (req, res) => { const countryCode = req.body.countryCode;
const catFacts = await getCatFacts();
const foxImage = await getFoxImage();
const dayOff = await getDayOff(countryCode);
return {
message: `Welcome to Node Babel with ${
req.body?.testValue ?? 'no testValue'
}`,
catFacts: catFacts,
foxImage: foxImage,
dayOff: dayOff,
};
});

Loading…
Cancel
Save