Browse Source

end project

main
Tristan 5 years ago
parent
commit
065b54b006
  1. 57
      index.js
  2. 3029
      package-lock.json

57
index.js

@ -1,15 +1,58 @@
import fastify from 'fastify';
// see axios doc on how to use it
import axios from 'axios';
const app = fastify({ logger: true });
app.get('/', async (req, res) => {
return {
message: `Welcome to Node Babel with ${
req.body?.testValue ?? 'no testValue'
}`,
};
const getcatfacts = () => {
return new Promise((resolve) => {
axios.get("https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=3").then((response) => {
resolve(response.data.map(res=>res.text))
}).catch((error) => {
resolve(null)
console.log(error)
})
})
}
const getfoximage = () => {
return new Promise((resolve) => {
axios.get("https://randomfox.ca/floof").then((response) => {
resolve(response.data.image)
}).catch((error) => {
resolve(null)
console.log(error)
})
}
)}
const getholiday = (country) => {
return new Promise((resolve) => {
axios.get(`https://date.nager.at/api/v2/PublicHolidays/2021/${country}`).then((response) => {
resolve(response.data)
}).catch((error) => {
resolve(null)
console.log(error)
})
}
)}
const fetchapi = async(country) => {
const catFacts = await getcatfacts();
const foxPicture = await getfoximage();
const holidays = await getholiday(country);
return Promise.all([catFacts, foxPicture, holidays]).then(() => {
return {
foxPicture,catFacts,holidays
}
})
}
app.post('/', async (req, res) => {
return fetchapi(req.body.countryCode)
});
// Run the server!

3029
package-lock.json
File diff suppressed because it is too large
View File

Loading…
Cancel
Save