airCondition.js
2.25 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var express = require("express");
var router = express.Router();
var axios = require("axios");
const openAPIKey = require("./secrets.json").openAPIKey;
const googleMapKey = require("./secrets.json").googleAPIKey;
axios.create({
// TODO : 웹을 AWS에 올릴때, 해당 baseURL이 달라져야할 수 있음
baseURL: "http://localhost:3001",
responseType: "json",
});
/* GET airCondition listing. */
router.get("/", async function (req, res, next) {
console.log("경도:", req.query.latitude);
console.log("경도:", req.query.longitude);
let airCondition = "";
let response = await getPosition(req.query.latitude, req.query.longitude)
.then((encodedStation) => getCondition(encodedStation))
.then((result) => {
airCondition = result;
});
res.send(airCondition);
});
const getPosition = (lat, lon) => {
return axios
.get(
"https://maps.googleapis.com/maps/api/geocode/json?latlng=" +
lat +
"," +
lon +
"&location_type=ROOFTOP&result_type=street_address&key=" +
googleMapKey +
"&language=ko"
)
.then(function (response) {
console.log("KEY : ", googleMapKey);
let stationName = "";
for (
let i = 0;
i < response["data"].results[0]["address_components"].length;
i++
) {
let temp =
response["data"].results[0]["address_components"][i]["long_name"];
if (temp[temp.length - 1] == "구") {
stationName = temp;
break;
}
}
console.log("STATION : ", stationName);
return (encodedStation = encodeURI(stationName));
})
.catch(function (error) {
console.log(error.response);
});
};
const getCondition = (encodedStation) => {
return axios
.get(
"http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getMsrstnAcctoRltmMesureDnsty?serviceKey=" +
openAPIKey +
"&numOfRows=10&pageNo=1&stationName=" +
encodedStation +
"&dataTerm=DAILY&ver=1.3&_returnType=json"
)
.then(function (response) {
// console.log("RES :: ", response);
result = response["data"]["list"][0];
return result;
})
.catch(function (error) {
console.log(error.response);
});
};
module.exports = router;