From 53496264d4d35b1ea6a9d8e148aedee5288e6e29 Mon Sep 17 00:00:00 2001 From: Juan Date: Mon, 24 Jan 2022 13:46:32 +0100 Subject: [PATCH] Exemple API en TD : team spacex --- b3-dev/api/api-php/infos.php | 2 + b3-dev/api/api-td-node/team-spacex/package.json | 17 +++++ b3-dev/api/api-td-node/team-spacex/server.js | 88 +++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 b3-dev/api/api-php/infos.php create mode 100644 b3-dev/api/api-td-node/team-spacex/package.json create mode 100644 b3-dev/api/api-td-node/team-spacex/server.js diff --git a/b3-dev/api/api-php/infos.php b/b3-dev/api/api-php/infos.php new file mode 100644 index 0000000..58ef35c --- /dev/null +++ b/b3-dev/api/api-php/infos.php @@ -0,0 +1,2 @@ + response.json()) +.then(function(data){ + // Récupération des données api + dataApi = data; +}).catch(function(error){ + console.log(error); +}); + + +// Renvoie à l'adresse http://localhost:3000/ +const server = http.createServer( // création du serveur + (request, response) => { + // création et envoi de la réponse + response.writeHead(200, {"Content-Type": "text/html"}); + response.write(JSON.stringify(dataApi)); + response.end(); + } +); + +server.listen(3000); + + +//Ancien code +// Création de l'entête de l'api +// function json_response(code, object, response) { +// response.statusCode = 200; +// response.setHeader('content-type', 'Application/json'); +// response.end(JSON.stringify(object)); +// } + +// let data = ""; +// const server = http.createServer(function(request, response) { +// let parseUrl = url.parse(request.url, true); + +// if(request.method === "GET") { +// console.log('GET'); +// data = getDragons(`https://api.spacexdata.com/v3/launches/60`); +// // Renvoyer la réponse JSON sur le navigateur +// console.log(data); +// } +// }); +// server.listen(3000); + + +// // Avec un objet simple +// server.on('request', (request, response) => { +// const { method, url} = request; +// const content_type = 'text/html; charset=utf-8'; +// response.setHeader('Content-Type', content_type); // (1) +// response.write(JSON.stringify(data)) +// }) + +// function getDragons(url){ +// let result = null; +// let promise = new Promise((resolve, reject) => { +// console.log(url); +// fetch(url).then(response => { +// if (response.ok) { +// response.json().then(data => resolve(data)); +// } else { +// response.json().then(data => reject(data)); +// } +// }); +// }); + +// promise.then((data) => { +// result = data; +// console.log(data); +// }, (err) => { +// if(!err.canceled) { +// console.log(err); +// } +// }) + +// return result; +// } + +