-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (45 loc) · 1.74 KB
/
server.js
File metadata and controls
55 lines (45 loc) · 1.74 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
const express = require("express");
const axios = require("axios");
const cors = require("cors");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 8080;
app.use(cors({ origin: "*" }));
// 책 검색 API
app.get("/api/searchBook", async (req, res) => {
const query = req.query.query;
const api_url = `https://openapi.naver.com/v1/search/book.json?query=${encodeURIComponent(query)}`;
try {
const response = await axios.get(api_url, {
headers: {
"X-Naver-Client-Id": process.env.REACT_APP_NAVER_CLIENT_ID,
"X-Naver-Client-Secret": process.env.REACT_APP_NAVER_CLIENT_SECRET,
},
});
res.status(200).json(response.data);
} catch (error) {
console.error("Error fetching data from Naver API:", error);
res.status(500).json({ message: "Error fetching data", error: error.message });
}
});
// 장소 검색 API
app.get("/api/searchPlace", async (req, res) => {
const query = req.query.query;
const api_url = `https://openapi.naver.com/v1/search/local.json?query=${encodeURIComponent(query)}&display=10`;
try {
const response = await axios.get(api_url, {
headers: {
"X-Naver-Client-Id": process.env.REACT_APP_NAVER_CLIENT_ID,
"X-Naver-Client-Secret": process.env.REACT_APP_NAVER_CLIENT_SECRET,
},
});
res.status(200).json(response.data);
} catch (error) {
console.error("Error fetching data from Naver API:", error);
res.status(500).json({ message: "Error fetching data", error: error.message });
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
module.exports = app;