Browse Source

index.js presque fini (fix la méthode get/post)

main
Abdelfettah 4 years ago
parent
commit
0436ca418c
No known key found for this signature in database GPG Key ID: C7FEEAE0315EBB77
  1. 65
      index.js
  2. 8535
      package-lock.json
  3. 5015
      yarn.lock

65
index.js

@ -4,16 +4,65 @@ import axios from 'axios';
const app = fastify({ logger: true }); const app = fastify({ logger: true });
app.post('/', async (req, res) => {
return {
message: `Welcome to Node Babel with ${
req.body?.testValue ?? 'no testValue'
}`,
};
let requestCat = async () => {
try {
const cat = await axios.get(`https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=3`);
const fact = cat.data.map((cat) => {
return cat.text;
}); });
return fact;
} catch (err) {
console.log("Error");
return null;
}
}
let requestFox = async () => {
try {
const fox = await axios.get('https://randomfox.ca/floof/');
return fox.data.image;
} catch (err) {
console.log("Error");
return null;
}
}
// Only used for dev server, do not remove
app.head('/', () => ({ ping: 'pong' }));
let requestHolidays = async (countryCode = 'FR') => {
try {
const holidays = await axios.get(`https://date.nager.at/api/v2/PublicHolidays/2021/${countryCode}`);
return holidays.data;
} catch (err) {
console.log("Error");
return null;
}
}
let fetchApis = async (countryCode) =>{
const catFacts = await requestCat();
const foxPicture = await requestFox();
const holidays = await requestHolidays(countryCode);
return {
foxPicture: foxPicture,
catFacts: catFacts,
holidays: holidays
}
}
app.post('/', async (req, res) => {
return await fetchApis(req.body.countryCode);
});
// Run the server! // Run the server!
const start = async () => { const start = async () => {

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

5015
yarn.lock
File diff suppressed because it is too large
View File

Loading…
Cancel
Save