server.js
1.91 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
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");
}
});
}
})