server.js 1.91 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');
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=A&numOfRows=12&pageNo=1&_type=json',
  'headers': {
  }
};
request(options, function (error, response, body) {
  if (error) {
      throw new Error(error);
  }
  let info = JSON.parse(body);

  

    server.get('/',(req,res)=>{
        const newFestival =new Festival();
        newFestival.title =info['response']['body']['items']['item'][11]['title'];
        newFestival.addr =info['response']['body']['items']['item'][11]['addr1'];
        newFestival.tel = info['response']['body']['items']['item'][11]['tel'];
        newFestival.mapx = info['response']['body']['items']['item'][11]['mapx'];
        newFestival.mapy =info['response']['body']['items']['item'][11]['mapy'];
        newFestival.save().then((festival)=>
        {
            console.log(festival);
            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");
            }
        });
    }
})