Browse Source

rendu tp1

main
zelleg 4 years ago
parent
commit
0a85752e6c
  1. 58
      index.js
  2. 3893
      package-lock.json

58
index.js

@ -4,17 +4,57 @@ 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'
}`,
};
});
async function foxImage() {
const url = "https://randomfox.ca/floof/";
try {
const response = await axios.get(url);
return response.data.image;
}
catch {
return null;
}
}
async function catFacts() {
const uri = 'https://cat-fact.herokuapp.com/facts/random?amount=3'
try {
const response = await axios.get(uri);
return response.data.map(x => x.text);
}
catch {
return null;
}
}
//gerer la partie ou le user ne definit pas le cr
async function dayOff(cr) {
const year = new Date().getFullYear();
const url = `https://date.nager.at/api/v3/PublicHolidays/${year}/${cr}`
try {
const response = await axios.get(url);
return response.data;
}
catch {
return null;
}
}
// Only used for dev server, do not remove // Only used for dev server, do not remove
app.head('/', () => ({ ping: 'pong' })); app.head('/', () => ({ ping: 'pong' }));
app.post('/', async (req, res) => {
// si jamais body est nul -> contrycode est nul
const countryCode = req.body?.countryCode;
return {
foxPicture: await foxImage(),
catFacts: await catFacts(),
holidays: await dayOff(countryCode)
//ajouter methode pour que les requêtes se lancent en même temps
}
});
// Run the server! // Run the server!
const start = async () => { const start = async () => {
try { try {
@ -24,4 +64,6 @@ const start = async () => {
process.exit(1); process.exit(1);
} }
}; };
start();
start();

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

Loading…
Cancel
Save