-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
94 lines (87 loc) · 2.74 KB
/
server.js
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
93
94
const axios = require("axios");
const express = require("express");
const app = express();
const port = 5000;
const path = require("path");
const bodyParser = require("body-parser");
require("dotenv").config();
const publicApiKey = process.env.REACT_APP_PUBLIC_KEY;
const kakaoApiKey = process.env.REACT_APP_KAKAO_KEY;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use("/weather_react", express.static(path.join(__dirname, "build")));
app.get("/weather_react", function (_, res) {
res.sendFile(path.join(__dirname, "build/index.html"));
});
app.post("/weather_react/area", async (req, res) => {
const { longitude, latitude } = req.body;
const url = `https://dapi.kakao.com/v2/local/geo/coord2regioncode.json?x=${longitude}&y=${latitude}`;
try {
const result = await axios({
method: "get",
url: url,
headers: {
Authorization: `KakaoAK ${kakaoApiKey}`,
},
});
res.send(result.data.documents);
} catch (error) {
console.error("area", error);
res.json({ message: "[Error] Fail get area data" });
}
});
const getPublicAPIdata = async (req, res) => {
const url = req.body.url;
const apiUrl = `${url}&serviceKey=${publicApiKey}`;
try {
const result = await axios.get(apiUrl);
const body = result.data.response.body;
if (body !== undefined) {
const items = body.items;
res.send(items);
} else {
const error = " response.body is undefined";
res.json({ message: error });
}
} catch (error) {
console.error("server error", error, apiUrl);
res.json({ message: "Fail fetch" });
}
};
app.post("/weather_react/sky", async (req, res) => {
await getPublicAPIdata(req, res);
});
app.post("/weather_react/usncst", async (req, res) => {
await getPublicAPIdata(req, res);
});
app.post("/weather_react/svfcast", async (req, res) => {
await getPublicAPIdata(req, res);
});
app.post("/weather_react/midFcast_landItems", async (req, res) => {
await getPublicAPIdata(req, res);
});
app.post("/weather_react/midFcast_taItems", async (req, res) => {
await getPublicAPIdata(req, res);
});
app.post("/weather_react/apNow", async (req, res) => {
await getPublicAPIdata(req, res);
});
app.post("/weather_react/apfcst", async (req, res) => {
await getPublicAPIdata(req, res);
});
app.post("/weather_react/sunInfo", async (req, res) => {
const url = req.body.url;
try {
const result = await axios.get(`${url}&ServiceKey=${publicApiKey}`);
const response = result.data.response;
const item = response.body.items.item;
res.send(item);
} catch (error) {
const e = `[Error]: fail fetch`;
console.error("sunInfo error", error);
res.json({ message: e });
}
});
app.listen(port, () => {
console.log("hello server", port);
});