From a0f5e24303cfbe21847b469fa2d423803e7fc795 Mon Sep 17 00:00:00 2001 From: Jimmy Bell <jimmy.bell@polymtl.ca> Date: Tue, 1 Dec 2020 15:59:26 -0500 Subject: [PATCH] Ajout du serveur, du script readjson fonctionnel et fuck david --- html/listeFrigos.html | 7 ++++++- scripts/readJson.js | 6 ++++++ server.js | 23 +++++++++++++++++++++++ src/ressources/frigos.json | 4 ++-- 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 scripts/readJson.js create mode 100644 server.js diff --git a/html/listeFrigos.html b/html/listeFrigos.html index 69836b2..0d63488 100644 --- a/html/listeFrigos.html +++ b/html/listeFrigos.html @@ -22,7 +22,7 @@ body{ <meta charset="UTF-8"> <title>Liste des frigos</title> </head> - <body> + <body id="listeFrigo"> <div class="fridgeOutline"> <span class="bold"> LIRE FRIGO @@ -33,4 +33,9 @@ body{ </body> +<script src="../scripts/readJson.js"> + var obj = httpGetJson(); + + document.getElementById("listeFrigo").innerHTML= +</script> </html> \ No newline at end of file diff --git a/scripts/readJson.js b/scripts/readJson.js new file mode 100644 index 0000000..d7a15ad --- /dev/null +++ b/scripts/readJson.js @@ -0,0 +1,6 @@ +function httpGetJson() { + var xmlHttpRequestLocal = new XMLHttpRequest(); + xmlHttpRequestLocal.open("GET", "http://127.0.0.1:3000/", false); + xmlHttpRequestLocal.send(null); + return JSON.parse(xmlHttpRequestLocal.responseText); +} \ No newline at end of file diff --git a/server.js b/server.js new file mode 100644 index 0000000..47ee93f --- /dev/null +++ b/server.js @@ -0,0 +1,23 @@ +const http = require('http'); +let fs = require('fs'); +let json = JSON.parse(fs.readFileSync('src/ressources/frigos.json')); + +const hostname = '127.0.0.1'; +const port = 3000; + +const server = http.createServer((req, res) => { + res.statusCode = 200; + res.setHeader('Content-Type', 'application/json'); + res.setHeader("Access-Control-Allow-Origin", "*"); + + if(req.method === 'GET'){ + console.log("GET") + res.end(JSON.stringify(json)) + } + res.statusCode = 405; + res.end() +}); + +server.listen(port, hostname, () => { + console.log(`Server running at http://${hostname}:${port}/`); +}); \ No newline at end of file diff --git a/src/ressources/frigos.json b/src/ressources/frigos.json index 3c2c13f..1868d37 100644 --- a/src/ressources/frigos.json +++ b/src/ressources/frigos.json @@ -1,4 +1,4 @@ -[ +{"array":[ { "nom": "Frigo Antoine", "addresse": "754 Rue Antoine, Montréal, Québec H4R 3T1", @@ -157,4 +157,4 @@ "repas": [ ] } -] +]} -- GitLab