Yield generated for 0501e9fe-1ce5-4e6c-8a6e-24fae01b2668
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

64 lines
1.4 KiB

import fastify from 'fastify';
// see axios doc on how to use it
import axios from 'axios';
const app = fastify({ logger: true });
const URLHOLIDAYS = `https://date.nager.at/api/v2/PublicHolidays/2021/`;
const URLCATFACTS= `https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=3`;
const URLFOX = `https://randomfox.ca/floof/`;
const getCatFacts = async () => {
try{
const response = await axios.get(URLCATFACTS);
const json = await response.data.map(cats=>cats.text);
return json;
}catch(err){
console.log(err);
return null;
}
};
const getFox = async()=>{
try{
const response = await axios.get(URLFOX);
const json = await response.data.image;
return json;
}catch(err){
console.log(err);
return null;
}
}
const getHolidays = async(country)=>{
try{
const response = await axios.get(`${URLHOLIDAYS}${country}`);
const data = await response.data;
return data;
}catch(err){
console.error(err);
return null;
}
}
app.get('/request', async (req, res) => {
const [catFacts,foxPicture,holidays]=
await Promise.all([getCatFacts(),getFox(),getHolidays("FR")])
return {
foxPicture,catFacts,holidays,
};
});
app.get('/', async (req, res) => {
return {
message: "Welcome to your projet"
};
});
const start = async () => {
try {
await app.listen(8090);
} catch (err) {
app.log.error(err);
process.exit(1);
}
};
start();