category.js
3.15 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
var express = require('express');
var router = express.Router();
var request = require('request');
const convert = require('xml-js');
require('dotenv').config();
var urlencode = require('urlencode');
/* GET home page. */
let GU_CODE;
let ANIMAL_INFO_API_KEY = process.env.REACT_APP_API_KEY;
let user_gu;
let user_latitude;
let user_longitude;
//let hospital_list = [];
// function list_print(res, menu_encode, gu_select_encode){
// //api
// let pet_url = `http://api.kcisa.kr/openapi/service/rest/convergence2019/getConver03?serviceKey=${ANIMAL_INFO_API_KEY}&numOfRows=100&pageNo=1&keyword=${menu_encode}&where=${gu_select_encode}`;
// request(pet_url, function(err, response, body){
// if(err) {
// console.log(`err => ${err}`)
// }
// else {
// if(res.statusCode == 200) {
// var result = convert.xml2json(body, {compact: true, spaces: 4});
// var petJson = JSON.parse(result)
// var itemList = petJson.response.body.items;
// var numRows = itemList.item.length; //개수
// for (i=0; i<numRows; i++){
// // state 정상인 것만 추리기
// if (itemList.item[i].state._text == '정상'){
// hospital_list.push(itemList.item[i]);
// }
// }
// //테스트용 console.log
// var titles = '';
// for(i=0; i<hospital_list.length; i++){
// titles = titles+hospital_list[i].title._text+'\n';
// }
// console.log(titles);
// }
// }
// res.render('result', { category: 'hospital', titles: titles, hospital_list: hospital_list });
// });
// }
router.post('/hospital', function(req, res){
//gu 받아오기
var body = req.body;
var gu_select = body.user_gu;
var menu = '동물병원';
// encoding for url
var menu_encode = urlencode(menu);
var gu_select_encode = urlencode(gu_select);
//list_print(res, menu_encode, gu_select_encode);
//api
let pet_url = `http://api.kcisa.kr/openapi/service/rest/convergence2019/getConver03?serviceKey=${ANIMAL_INFO_API_KEY}&numOfRows=100&pageNo=1&keyword=${menu_encode}&where=${gu_select_encode}`;
request(pet_url, function(err, response, body){
if(err) {
console.log(`err => ${err}`)
}
else {
if(res.statusCode == 200) {
var hospital_list = []; //얘를 전역으로 선언해서 중복이 발생했던 거였어요 ㅠ
var titles = '';
var result = convert.xml2json(body, {compact: true, spaces: 4});
var petJson = JSON.parse(result)
var itemList = petJson.response.body.items;
var numRows = itemList.item.length; //개수
for (i=0; i<numRows; i++){
// state 정상인 것만 추리기
if (itemList.item[i].state._text == '정상'){
hospital_list.push(itemList.item[i]);
}
}
//테스트용 console.log
for(i=0; i<hospital_list.length; i++){
titles = titles+hospital_list[i].title._text+'\n';
}
console.log(titles);
}
}
res.render('result', { category: 'hospital', titles: titles, hospital_list: hospital_list });
})
});
module.exports = router;