Solhee

InvalidInputProcessingForFoodAdtvAPI

1 var express = require('express'); 1 var express = require('express');
2 var request = require('request'); 2 var request = require('request');
3 -
4 //xml을 json으로 바꾼다. 3 //xml을 json으로 바꾼다.
5 const convert = require('xml-js'); 4 const convert = require('xml-js');
6 5
6 +var ProductCategoryName = '스위트초콜릿';
7 +
7 var url = 'http://apis.data.go.kr/1470000/FoodAdtvInfoService/getFoodAdtvInfoList'; 8 var url = 'http://apis.data.go.kr/1470000/FoodAdtvInfoService/getFoodAdtvInfoList';
8 var key ='ofY2ppOq5kBqT5jYPaGsW%2BEy7OR5a1bf5Z9PHvqNKvwO5DSCaU2x2qCj%2FoXnuB1YVbMTlErkHWSMEsR5b7isrw%3D%3D'; 9 var key ='ofY2ppOq5kBqT5jYPaGsW%2BEy7OR5a1bf5Z9PHvqNKvwO5DSCaU2x2qCj%2FoXnuB1YVbMTlErkHWSMEsR5b7isrw%3D%3D';
9 var queryParams = '?' + encodeURIComponent('ServiceKey') + '=' +key; 10 var queryParams = '?' + encodeURIComponent('ServiceKey') + '=' +key;
10 //queryParams += '&' + encodeURIComponent('prdlst_cd') + '=' + encodeURIComponent('C0118010300000'); 11 //queryParams += '&' + encodeURIComponent('prdlst_cd') + '=' + encodeURIComponent('C0118010300000');
11 -queryParams += '&' + encodeURIComponent('pc_kor_nm');// + '=' + encodeURIComponent('스위트초콜릿'); 12 +queryParams += '&' + encodeURIComponent('pc_kor_nm') + '=' + encodeURIComponent(ProductCategoryName);
12 -queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('213'); 13 +//queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent('213');
13 -queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('100'); 14 +//queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent('100');
14 15
15 -global.foods = new Array();
16 16
17 17
18 request({ 18 request({
...@@ -26,25 +26,34 @@ request({ ...@@ -26,25 +26,34 @@ request({
26 if(response.statusCode ==200){ 26 if(response.statusCode ==200){
27 var result =body; 27 var result =body;
28 var xmltojson = convert.xml2json(result,{compact:true,spaces:4}); 28 var xmltojson = convert.xml2json(result,{compact:true,spaces:4});
29 - //console.log(xmltojson);
30 var resObj = eval("("+xmltojson+")"); 29 var resObj = eval("("+xmltojson+")");
31 var resultItems = resObj.response.body.items; 30 var resultItems = resObj.response.body.items;
32 - //console.log(xmltojson); 31 + var resultLeng = Object.keys(resultItems);
33 32
33 + if(Object.keys(resultItems).length != 0){
34 + console.log("valid input : 식품 첨가물 정보를 받아옵니다.");
34 35
36 + var responseMessage ='[ ' + resultItems.item[0].PC_KOR_NM._text + ' ]\n';
37 + //하나의 카테고리에 대한 정보만 받는다고 가정해 반복문 밖으로 뺐습니다.
35 38
36 - for(var i=0 ; i < resultItems.item.length; i ++){ 39 + for(var i=0 ; i < resultItems.item.length; i ++)
37 - var responseMessage ='[ ' + resultItems.item[i].PC_KOR_NM._text + ' ]\n'; 40 + {
41 + //var responseMessage ='[ ' + resultItems.item[i].PC_KOR_NM._text + ' ]\n';
38 var addictive = resultItems.item[i].T_KOR_NM._text; 42 var addictive = resultItems.item[i].T_KOR_NM._text;
39 var specVal = resultItems.item[i].SPEC_VAL_SUMUP._text; 43 var specVal = resultItems.item[i].SPEC_VAL_SUMUP._text;
40 var yn = resultItems.item[i].INJRY_YN._text; 44 var yn = resultItems.item[i].INJRY_YN._text;
41 responseMessage += addictive + ' : ' + specVal + '['+yn+']'+ '\n'; 45 responseMessage += addictive + ' : ' + specVal + '['+yn+']'+ '\n';
42 - console.log('[responese message]',responseMessage);
43 } 46 }
44 47
45 } 48 }
49 + else
50 + {
51 + console.log("Invalid Input : 에러 메시지 전송");
52 + var responseMessage = "잘못 된 입력입니다. 라벨의 식품유형을 확인하고 다시 입력해주세요.";
53 + }
54 + console.log('[responese message]',responseMessage);
55 +
56 + }
46 } 57 }
47 - //console.log('Status', response.statusCode); 58 +
48 - //console.log('Headers', JSON.stringify(response.headers));
49 - //console.log('Reponse received', body);
50 }); 59 });
......