박주영

Delete foodapi.js

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