hellowhales

Implement API handler

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);
for (i in info['response']['body']['items']['item']) {
console.log('축제 : ' + info['response']['body']['items']['item'][i]['title']);
console.log('축제 주소 : ' + info['response']['body']['items']['item'][i]['addr1']);
console.log(
'전화번호 : ' + info['response']['body']['items']['item'][i]['tel']
);
console.log('축제위도 : ' + info['response']['body']['items']['item'][i]['mapx']);
console.log('축제경도 : ' + info['response']['body']['items']['item'][i]['mapy']);
console.log(" ")
}
});
\ No newline at end of file
const mongoose =require('mongoose');
const { Schema } =mongoose;
const festivalSchema = new Schema({
title: String,
addr: String,
tel: String,
mapx : Number,
mapy : Number
},
{
timestamps: true
}
);
module.exports = mongoose.model('Festival',festivalSchema);
This diff is collapsed. Click to expand it.
{
"name": "tft",
"version": "0.0.0",
"private": true,
"name": "REST-API",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node ./bin/www"
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"ejs": "~2.6.1",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"morgan": "~1.9.1"
"dotenv": "^10.0.0",
"express": "^4.17.1",
"mongoose": "^6.0.13",
"request": "^2.88.2"
}
}
......
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");
}
});
}
})
MONGODB_URL = mongodb+srv://hellowhales:qogudtjr`12@cluster0.7gz7l.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
\ No newline at end of file