hellowhales

Implement API handler

1 +let request = require('request');
2 +let options = {
3 + 'method': 'GET',
4 + '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',
5 + 'headers': {
6 + }
7 +};
8 +request(options, function (error, response, body) {
9 + if (error) {
10 + throw new Error(error);
11 + }
12 + let info = JSON.parse(body);
13 +
14 + for (i in info['response']['body']['items']['item']) {
15 + console.log('축제 : ' + info['response']['body']['items']['item'][i]['title']);
16 + console.log('축제 주소 : ' + info['response']['body']['items']['item'][i]['addr1']);
17 + console.log(
18 + '전화번호 : ' + info['response']['body']['items']['item'][i]['tel']
19 + );
20 + console.log('축제위도 : ' + info['response']['body']['items']['item'][i]['mapx']);
21 + console.log('축제경도 : ' + info['response']['body']['items']['item'][i]['mapy']);
22 + console.log(" ")
23 + }
24 +});
25 +
...\ No newline at end of file ...\ No newline at end of file
1 +const mongoose =require('mongoose');
2 +const { Schema } =mongoose;
3 +
4 +const festivalSchema = new Schema({
5 + title: String,
6 + addr: String,
7 + tel: String,
8 + mapx : Number,
9 + mapy : Number
10 +},
11 +{
12 + timestamps: true
13 +}
14 +);
15 +
16 +
17 +module.exports = mongoose.model('Festival',festivalSchema);
This diff is collapsed. Click to expand it.
1 { 1 {
2 - "name": "tft", 2 + "name": "REST-API",
3 - "version": "0.0.0", 3 + "version": "1.0.0",
4 - "private": true, 4 + "description": "",
5 + "main": "index.js",
5 "scripts": { 6 "scripts": {
6 - "start": "node ./bin/www" 7 + "test": "echo \"Error: no test specified\" && exit 1"
7 }, 8 },
9 + "keywords": [],
10 + "author": "",
11 + "license": "ISC",
8 "dependencies": { 12 "dependencies": {
9 - "cookie-parser": "~1.4.4", 13 + "dotenv": "^10.0.0",
10 - "debug": "~2.6.9", 14 + "express": "^4.17.1",
11 - "ejs": "~2.6.1", 15 + "mongoose": "^6.0.13",
12 - "express": "~4.16.1", 16 + "request": "^2.88.2"
13 - "http-errors": "~1.6.3",
14 - "morgan": "~1.9.1"
15 } 17 }
16 } 18 }
......
1 +const express = require('express');
2 +const mongoose =require('mongoose');
3 +const server = express();
4 +const Festival =require('./models/Festival');
5 +require("dotenv").config({ path: "variables.env"});
6 +
7 +let request = require('request');
8 +let options = {
9 + 'method': 'GET',
10 + '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',
11 + 'headers': {
12 + }
13 +};
14 +request(options, function (error, response, body) {
15 + if (error) {
16 + throw new Error(error);
17 + }
18 + let info = JSON.parse(body);
19 +
20 +
21 +
22 + server.get('/',(req,res)=>{
23 + const newFestival =new Festival();
24 + newFestival.title =info['response']['body']['items']['item'][11]['title'];
25 + newFestival.addr =info['response']['body']['items']['item'][11]['addr1'];
26 + newFestival.tel = info['response']['body']['items']['item'][11]['tel'];
27 + newFestival.mapx = info['response']['body']['items']['item'][11]['mapx'];
28 + newFestival.mapy =info['response']['body']['items']['item'][11]['mapy'];
29 + newFestival.save().then((festival)=>
30 + {
31 + console.log(festival);
32 + res.json({
33 + message:'Festival Created Successfully'
34 + });
35 + })
36 + .catch((err)=>
37 + {
38 + res.json({
39 + message:'Festival was not successfully created'
40 + });
41 + });
42 + });
43 +});
44 +
45 +server.listen(3000,(err)=>{
46 + if(err){
47 + return console.log(err);
48 + }else{
49 + mongoose.connect(process.env.MONGODB_URL,{useNewUrlParser:true},(err)=>{
50 + if(err){
51 + console.log(err);
52 + }else{
53 + console.log("Connected to database successfully");
54 + }
55 + });
56 + }
57 +})
1 +MONGODB_URL = mongodb+srv://hellowhales:qogudtjr`12@cluster0.7gz7l.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
...\ No newline at end of file ...\ No newline at end of file