From 7b17b7144281a8c39de838fa89004e7fda3fd597 Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 15 Feb 2021 17:22:34 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20cat=20facts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index a282a8e..91e7f05 100644 --- a/index.js +++ b/index.js @@ -4,11 +4,28 @@ import axios from 'axios'; const app = fastify({ logger: true }); -app.get('/', async (req, res) => { +function timeout(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +// const getCat = async () => { +// console.log('toto'); +// let response = await axios.get(`https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=3`) +// .then(res => response.json()); +// }; + +async function getCat() { + const response = await axios.get("https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=3"); + const data = await response.data; + const text = data.map((item => item.text)); + return text; +} + +app.post('/', async (req, res) => { + const catInfos = await getCat(); return { - message: `Welcome to Node Babel with ${ - req.body?.testValue ?? 'no testValue' - }`, + message: `Welcome to Node Babel`, + catFacts: catInfos }; }); @@ -22,3 +39,5 @@ const start = async () => { } }; start(); + +