Yield generated for 9ac5ed7e-f78b-4a72-a8cd-f82cf0ad7a54
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.
 
 

44 lines
1.1 KiB

import fastify from 'fastify';
// see axios doc on how to use it
import axios from 'axios';
const app = fastify({ logger: true });
const getFoxImg = () => axios.get('https://randomfox.ca/floof/').then(resp => resp.data.image).catch(() => null);
const getCatFacts = (nb) => axios.get( `https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=${nb}`).then(resp => resp.data.map(catInfo => catInfo.text)).catch(() => null );
const getHolidays = (year , contryCode) => axios.get( `https://date.nager.at/api/v3/publicholidays/${year}/${contryCode}`).then(resp => resp.data).catch(() => null);
app.post('/' , async (req, res) => {
let imgFox = await getFoxImg();
let catFacts = await getCatFacts(3);
let holidays = await getHolidays(2022 ,req.body.countryCode);
return {
imgFox,
catFacts,
holidays
};
})
// Only used for dev server, do not remove
app.head('/', () => ({ ping: 'pong' }));
// Run the server!
const start = async () => {
try {
await app.listen(5000);
} catch (err) {
app.log.error(err);
process.exit(1);
}
};
start();