-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (55 loc) · 2.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//Server Side
const fetch = require("node-fetch");
var express = require("express");
var socket = require("socket.io");
// App Setup
var app = express();
var server = app.listen(4000, function () {
console.log("listening to requests on port 4000")
});
//Client sees public folder
app.use(express.static('public'));
//Socket Setup
var io = socket(server);
//Needed Variables for better protection of data
var apiKey = "1868ff532a12f7795015bc33a64c2e97";
var half_url_API = "https://api.darksky.net/forecast/" + apiKey + "/";
var half_url_city = "https://eu1.locationiq.com/v1/reverse.php?key=d2c1d2b99010ab&normalizecity=1&lat=";
var half_url_search_city = "https://eu1.locationiq.com/v1/search.php?key=d2c1d2b99010ab&q=city,";
io.on("connection", function (socket) {
console.log("made socket connection");
//When Server recieves "urls" message...
socket.on("urls", function (urls) {
console.log("got urls for reverse geocoding & weather API");
fetch(half_url_city + urls.url_city).then(
(result) => {
return result.json()
}
).then((json) => {
var cityname = json["address"]["city"] + ", " + json["address"]["country"];
fetch(half_url_API + urls.url_API).then(
(result) => {
return result.json()
}
).then((json) => {
socket.emit("urls", {url_API: json, cityname: cityname});
console.log("sent url-data to client")
}
).catch((err) => console.log(err));
}
).catch((err) => console.log(err));
});
socket.on("search_city", function (url_search_city, ackCallback) {
console.log("got search-city request");
fetch(half_url_search_city + url_search_city.url_searchCity).then(
(result) => {
return result.json()
}
).then( (json) =>{
console.log("sent lat&long to client");
var searchLatLong = json[0]["lat"] + "," + json[0]["lon"];
ackCallback(searchLatLong)
}
).catch((err) => console.log(err));
})
});