server.js
4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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");
}
});
}
})