server.js 4.01 KB
const express = require('express');
const mongoose = require('mongoose');
const server = express();
const Festival = require('./models/Festival');
require("dotenv").config({ path: "variables.env"});

let request = require('request-promise-native');
let options = {
  'method': 'GET',
  'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/areaBasedList?ServiceKey=2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D&contentTypeId=15&areaCode=&sigunguCode=&cat1=&cat2=&cat3=&listYN=Y&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&arrange=C&numOfRows=12&pageNo=1&_type=json',
  'headers': {
  }
};
var contentId = new Array();

request(options, function (error, response, body) {
  if (error) {
      throw new Error(error);
  }
  let info = JSON.parse(body);

  for(i in info['response']['body']['items']['item']){
        contentId[i]=info['response']['body']['items']['item'][7]['contentid'];
        let Info ={
            'public':{
                'method': 'GET',
                'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailCommon?ServiceKey=2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D&contentTypeId=15&contentId='+contentId[i]+'&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&defaultYN=Y&firstImageYN=Y&areacodeYN=Y&catcodeYN=Y&addrinfoYN=Y&mapinfoYN=Y&overviewYN=Y&transGuideYN=Y&_type=json',
                'headers': {
                }
            },
            'detail':{
                'method': 'GET',
                'url': 'http://api.visitkorea.or.kr/openapi/service/rest/KorService/detailIntro?ServiceKey=2lFkvQJYgzOOhwUKiUt8aZVNpd1PpBOf%2FfMNW17cl25DE0GUEDddeR9iGnuSUpggjUoIUgamfhcvnKQ3eH1dAw%3D%3D&contentTypeId=15&contentId='+contentId[i]+'&MobileOS=ETC&MobileApp=TourAPI3.0_Guide&introYN=Y&_type=json',
                'headers': {
                }
            }
        };
  
    request(Info.public,function (error, response, body) {
        if (error) {
          throw new Error(error);
      }
      let pinfo = JSON.parse(body);
      server.get('/', async (req,res)=>{
            const newFestival = new Festival();
            await request(Info.detail, function (error, response, body) {
                if (error) {
                    throw new Error(error);
                }
                let dinfo = JSON.parse(body); 
                newFestival.eventstartdate = dinfo['response']['body']['items']['item']['eventstartdate'];
                newFestival.eventenddate = dinfo['response']['body']['items']['item']['eventenddate'];
            });     
            newFestival.title = pinfo['response']['body']['items']['item']['title'];
            newFestival.addr = pinfo['response']['body']['items']['item']['addr1'];
            newFestival.tel = pinfo['response']['body']['items']['item']['tel'];
            newFestival.mapx = pinfo['response']['body']['items']['item']['mapx'];
            newFestival.mapy = pinfo['response']['body']['items']['item']['mapy'];
            newFestival.overview= pinfo['response']['body']['items']['item']['overview'];
            newFestival.firstimage = pinfo['response']['body']['items']['item']['firstimage'];
            newFestival.homepage = pinfo ['response']['body']['items']['item']['homepage'];
         
            newFestival.save().then((festival) =>
            {
                console.log(festival, "Save success!");
                res.json({
                    message:'Festival Created Successfully'
                });
            })
            .catch((err)=>
            {
                res.json({
                    message:'Festival was not successfully created'
                });
            });
        });
    });
}});

server.listen(3000,(err)=>{
    if(err){
        return console.log(err);
    }else{
        mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{
            if(err){
                console.log(err);
            }else{
                console.log("Connected to database successfully");
            }
        });
    }
})