Browse Source
index.js presque fini (fix la méthode get/post)
main
Abdelfettah
4 years ago
No known key found for this signature in database
GPG Key ID: C7FEEAE0315EBB77
3 changed files with
11047 additions and
2568 deletions
-
index.js
-
package-lock.json
-
yarn.lock
|
|
|
@ -4,16 +4,65 @@ import axios from 'axios'; |
|
|
|
|
|
|
|
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!
|
|
|
|
const start = async () => { |
|
|
|
|