main.js
7.09 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// api에서 데이터 불러오기
var xml2js = require('xml-js');
var urlencode = require('urlencode');
var xmlToJson;
// 변수들
var City; // 도시 이름
var Feb_Dust_2018 =0 ;
var Mar_Dust_2018 =0 ;
var April_Dust_2018 =0 ;
var May_Dust_2018 =0 ;
var June_Dust_2018 =0 ;
var Feb_Dust_2019 =0 ;
var Mar_Dust_2019 =0 ;
var April_Dust_2019 =0 ;
var May_Dust_2019 =0 ;
var June_Dust_2019 =0 ;
var Feb_Dust_2020 =0 ;
var Mar_Dust_2020 =0 ;
var April_Dust_2020 =0 ;
var May_Dust_2020 =0 ;
var June_Dust_2020 =0 ;
var check = false;
var url;
var Dust_Place //측정 도시
const request = require('request');
const Dust_URL = "http://openAPI.seoul.go.kr:8088/" //미세먼지 api주소_1
const Dust_URL2 = "/xml/MonthlyAverageAirQuality/1/5/" //미세먼지 api주소_2
const Dust_ServiceKey = "73754a53586a696d38345951716b6b" //api의 key값
module.exports = function(app)
{
app.get('/',function(req,res){
res.render('City_login')
});
app.get('/city',function(req,res){
//2020년 2월~6월 미세먼지 평균
function DelayNode_2020(item)
{
City = req.query.city;
return new Promise(function(resolve,reject){
setTimeout(function(){
Dust_Place ='/'+ urlencode(req.query.city)
url = Dust_URL+Dust_ServiceKey+Dust_URL2+item+Dust_Place;
request(url,function(error,res,body) {
if(error){
console.log('error=>${error}');
}
else{
if(res.statusCode == 200) {
var result = body;
xmlToJson = xml2js.xml2json(result, {compact: true, spaces: 4});
xmlToJson2 = JSON.parse(xmlToJson);
if(item == 202002){
Feb_Dust_2020 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
}
else if(item == 202003){
Mar_Dust_2020 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
}
else if(item == 202004){
April_Dust_2020 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
}
else if(item == 202005){
May_Dust_2020 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
}
else if(item == 202006){
June_Dust_2020 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
}
}
}
});
resolve();
})
})
}
async function Dust_Month_2020(Dust_Month_2020){
for(let i=0;i<Dust_Month_2020.length;i++)
{
await DelayNode_2020(Dust_Month_2020[i]);
}
}
Dust_Month_2020([202002,202003,202004,202005,202006]);
//2019년 2월~6월 미세먼지 평균
function DelayNode_2019(item)
{
return new Promise(function(resolve,reject){
setTimeout(function(){
Dust_Place ='/'+ urlencode(req.query.city)
url = Dust_URL+Dust_ServiceKey+Dust_URL2+item+Dust_Place;
console.log(url)
request(url,function(error,res,body) {
if(error){
console.log('error=>${error}');
}
else{
if(res.statusCode == 200) {
var result = body;
xmlToJson = xml2js.xml2json(result, {compact: true, spaces: 4});
xmlToJson2 = JSON.parse(xmlToJson);
if(item == 201902){
Feb_Dust_2019 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
}
else if(item == 201903){
Mar_Dust_2019 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
}
else if(item == 201904){
April_Dust_2019 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
}
else if(item == 201905){
May_Dust_2019 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
}
else if(item == 201906){
June_Dust_2019 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
}
}
}
});
resolve();
})
})
}
async function Dust_Month_2019(Dust_Month_2019){
for(let i=0;i<Dust_Month_2019.length;i++)
{
await DelayNode_2019(Dust_Month_2019[i]);
}
}
Dust_Month_2019([201902,201903,201904,201905,201906]);
//2018년 2월~6월 미세먼지 평균
function DelayNode_2018(item)
{
return new Promise(function(resolve,reject){
setTimeout(function(){
Dust_Place ='/'+ urlencode(req.query.city)
url = Dust_URL+Dust_ServiceKey+Dust_URL2+item+Dust_Place;
request(url,function(error,res,body) {
if(error){
console.log('error=>${error}');
}
else{
if(res.statusCode == 200) {
var result = body;
xmlToJson = xml2js.xml2json(result, {compact: true, spaces: 4});
xmlToJson2 = JSON.parse(xmlToJson);
if(item == 201802){
Feb_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
console.log(1)
}
else if(item == 201803){
Mar_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
console.log(2)
}
else if(item == 201804){
April_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
console.log(3)
}
else if(item == 201805){
May_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
console.log(4)
}
else if(item == 201806){
June_Dust_2018 = parseInt(xmlToJson2["MonthlyAverageAirQuality"]["row"]["PM10"]["_text"])
console.log(5)
console.log("Setting Complete!!!")
check = true;
}
}
}
});
resolve();
})
})
}
async function Dust_Month_2018(Dust_Month_2018){
for(let i=0;i<Dust_Month_2018.length;i++)
{
await DelayNode_2018(Dust_Month_2018[i]);
}
}
Dust_Month_2018([201802,201803,201804,201805,201806]);
//값을 다 받고난뒤 index.ejs에 값 보내주기
if(check == true){
res.render('index',{
Feb_Dust_2018,
Mar_Dust_2018,
April_Dust_2018,
May_Dust_2018,
June_Dust_2018,
Feb_Dust_2019,
Mar_Dust_2019,
April_Dust_2019,
May_Dust_2019,
June_Dust_2019,
Feb_Dust_2020,
Mar_Dust_2020,
April_Dust_2020,
May_Dust_2020,
June_Dust_2020,
City
})
}
});
}