Browse Source

end of TP, api is working well

main
Thais Revillon 5 years ago
parent
commit
874ee5b671
  1. 42
      index.js

42
index.js

@ -1,14 +1,44 @@
import fastify from 'fastify'; import fastify from 'fastify';
// see axios doc on how to use it
import axios from 'axios';
import axios from 'axios'
const app = fastify({ logger: true }); const app = fastify({ logger: true });
app.get('/', async (req, res) => {
const getCats = async () => {
try {
const {data} = await axios.get('https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=3');
return data.map(({text}) => text)
} catch (err) {
console.error(err);
return null;
}
}
const getFox = async () => {
try {
const { data: {image } }= await axios.get('https://randomfox.ca/floof/');
return image;
} catch (err) {
console.error(err);
return null;
}
}
const getHolidays = async (country, year ) => {
try {
const {data} = await axios.get(`https://date.nager.at/api/v2/PublicHolidays/${year}/${country} `);
return data;
} catch (err) {
console.error(err);
return null;
}
}
app.post('/', async (req, res) => {
const [cats, fox, holidays] = await Promise.all([getCats(), getFox(), getHolidays(req.body?.countryCode ?? 'FR', req.body?.year ?? '2021')]);
return { return {
message: `Welcome to Node Babel with ${
req.body?.testValue ?? 'no testValue'
}`,
cats,
fox,
holidays
}; };
}); });

Loading…
Cancel
Save