From 68923d54a8f526ebc28fb655331f4d894f96305c Mon Sep 17 00:00:00 2001 From: Juan Date: Thu, 16 Dec 2021 16:55:04 +0100 Subject: [PATCH] =?UTF-8?q?server=20exemple=20pour=20aller=20chercher=20la?= =?UTF-8?q?=20m=C3=A9t=C3=A9o=20=C3=A0=20une=20address?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- b3-dev/api/session1/package.json | 7 ++++ b3-dev/api/session1/server.js | 71 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 b3-dev/api/session1/package.json create mode 100644 b3-dev/api/session1/server.js diff --git a/b3-dev/api/session1/package.json b/b3-dev/api/session1/package.json new file mode 100644 index 0000000..e97c680 --- /dev/null +++ b/b3-dev/api/session1/package.json @@ -0,0 +1,7 @@ +{ + "type": "module", + "dependencies": { + "node-fetch": "^3.1.0", + "nodemon": "^2.0.15" + } +} diff --git a/b3-dev/api/session1/server.js b/b3-dev/api/session1/server.js new file mode 100644 index 0000000..08c0855 --- /dev/null +++ b/b3-dev/api/session1/server.js @@ -0,0 +1,71 @@ +import http from 'http'; +import * as url from "url"; +import fetch from 'node-fetch'; + +function json_response(code, object, response) { + response.statusCode = 200; + response.setHeader('content-type', 'Application/json'); + response.end(JSON.stringify(object)); +} + +const server = http.createServer(function (request, response) { + let parsedUrl = url.parse(request.url, true); + if(request.method === "POST") { + console.log('POST'); + if(parsedUrl.query.param) + { + json_response(200,parsedUrl.query,response); + } + else + { + json_response(400,{error:"missing required param"},response); + } + } + else if(request.method === "PUT") { + console.log('PUT'); + json_response(200,{status : "where should I put it ?"},response); + } + else if(request.method === "DELETE") { + console.log('DELETE'); + json_response(200,{status : "no delete available"},response); + } + else + { + console.log(request.method); + console.log(parsedUrl.query.address); + getLatLong(parsedUrl.query.address); + json_response(200,{status : "active"},response); + } +}); +server.listen(8080); + +function getLatLong(address) { + + let result = null; + /* Create a new promise and send geocoding request */ + var promise = new Promise((resolve, reject) => { + var apiKey = "b814900746f646f0a21765274c98f387"; + var url = `https://api.geoapify.com/v1/geocode/search?text=${encodeURIComponent(address)}&apiKey=${apiKey}`; + 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) => { + console.log(data.features[0].properties); + getWeatherAtLatLong(data.features[0].properties.lat, data.features[0].properties.lon); + }, (err) => { + if (!err.canceled) { + console.log(err); + } + }); +} + +function getWeatherAtLatLong(lat,long) { + console.log('getWeatherAtLatLong('+lat+','+long+')'); +}