Solhee

Merge branch 'api' of http://khuhub.khu.ac.kr/term-project/project into master

1 -let request = require('request'); 1 +var express = require('express');
2 +var request = require('request');
2 3
3 -// REST API 경로 & 요청 메시지 4 +//xml을 json으로 바꾼다.
4 -const url = 'http://apis.data.go.kr/1470000/FoodAdtvInfoService/getFoodAdtvInfoList?ServiceKey='; 5 +const convert = require('xml-js');
5 6
6 -//api key 7 +var url = 'http://apis.data.go.kr/1470000/FoodAdtvInfoService/getFoodAdtvInfoList';
7 -const key = 'ofY2ppOq5kBqT5jYPaGsW%2BEy7OR5a1bf5Z9PHvqNKvwO5DSCaU2x2qCj%2FoXnuB1YVbMTlErkHWSMEsR5b7isrw%3D%3D'; 8 +var key ='ofY2ppOq5kBqT5jYPaGsW%2BEy7OR5a1bf5Z9PHvqNKvwO5DSCaU2x2qCj%2FoXnuB1YVbMTlErkHWSMEsR5b7isrw%3D%3D';
9 +var queryParams = '?' + encodeURIComponent('ServiceKey') + '=' +key;
10 +//queryParams += '&' + encodeURIComponent('prdlst_cd') + '=' + encodeURIComponent('C0118010300000');
11 +//queryParams += '&' + encodeURIComponent('pc_kor_nm') + '=' + encodeURIComponent('과.채음료');
12 +//queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('3');
13 +queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('100');
8 14
9 -//REST(URL) 15 +global.foods = new Array();
10 -const complete_url = url +key ;// '&numOfRows=2&pageNo=2';
11 16
12 -console.log(complete_url);
13 17
14 -request(complete_url, function(error, response, body){
15 - if(!error && response.statusCode ==200)
16 - console.log(body);
17 -})
18 -
19 -
20 -/*
21 -queryParams += '&' + encodeURIComponent('prdlst_cd') + '=' + encodeURIComponent('C0118010300000');
22 -queryParams += '&' + encodeURIComponent('pc_kor_nm') + '=' + encodeURIComponent('과.채음료');
23 -queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('1');
24 -queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('3');
25 -*/
26 -
27 -
28 -/*
29 -예제코드
30 request({ 18 request({
31 url: url + queryParams, 19 url: url + queryParams,
32 method: 'GET' 20 method: 'GET'
33 }, function (error, response, body) { 21 }, function (error, response, body) {
22 + if(error){
23 + console.log('에러입니다.')
24 + }
25 + else{
26 + if(response.statusCode ==200)
27 + var result =body;
28 + var xmltojson = convert.xml2json(result,{compact:true,spaces:4});
29 + //console.log(xmltojson);
30 + var resObj = eval("("+xmltojson+")");
31 +
32 + for(var i=0;i<100;i++){
33 + function fo(){
34 + foods.push(food);
35 + }
36 + function foo(){
37 + var food =resObj.response.body.items.item[i].PC_KOR_NM._text;
38 + fo();
39 + }
40 + var food = resObj.response.body.items.item[i].PC_KOR_NM._text;
41 + global.foods.push(food);
42 + //foods.food = resObj.response.body.items.item[i].PC_KOR_NM._text;
43 + //foods.push(resObj.response.body.items.item[i].PC_KOR_NM._text);
44 + //console.log(resObj.response.body.items.item[i].PC_KOR_NM._text);
45 + }
46 + //console.log(xmltojson);
47 + }
34 //console.log('Status', response.statusCode); 48 //console.log('Status', response.statusCode);
35 //console.log('Headers', JSON.stringify(response.headers)); 49 //console.log('Headers', JSON.stringify(response.headers));
36 //console.log('Reponse received', body); 50 //console.log('Reponse received', body);
51 + console.log(foods[99]);
37 }); 52 });
38 -*/
39 53
40 -// module.exports.이름 = 함수; 54 +console.log(foods);
...\ No newline at end of file ...\ No newline at end of file
......