Showing
2 changed files
with
0 additions
and
117 deletions
server.js
deleted
100644 → 0
1 | -const express = require("express"); | ||
2 | -const app = express(); | ||
3 | -const request = require("request"); | ||
4 | -const convert = require("xml-js"); | ||
5 | -const fs = require("fs"); | ||
6 | -const xml2js = require("xml2js"); | ||
7 | - | ||
8 | -// Modify the values as needed | ||
9 | -var year = "2022"; | ||
10 | -var month = "09"; | ||
11 | -var operation = "getHoliDeInfo"; | ||
12 | - | ||
13 | -// Do not modify the values | ||
14 | -var SERVEICE_KEY = | ||
15 | - "qBtJy2Prw8CCnAiijUM7VkuaA9MZozHuiQI4FbEGYdUDPz4%2FM%2FuxegGjNBWK0aWQHvSslVHwIZQwNWh57WgRTA%3D%3D"; | ||
16 | -var url = | ||
17 | - "http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/" + | ||
18 | - operation; | ||
19 | -var queryParams = "?" + "solYear" + "=" + year; | ||
20 | -queryParams += "&" + "solMonth" + "=" + month; | ||
21 | -queryParams += "&" + "ServiceKey" + "=" + SERVEICE_KEY; | ||
22 | -let requestUrl = url + queryParams; | ||
23 | - | ||
24 | -// Empty variables | ||
25 | -var text = ""; | ||
26 | -var dateName = []; | ||
27 | -var locdate = []; | ||
28 | -var tempArr = []; | ||
29 | - | ||
30 | -// To run EJS engine | ||
31 | -app.set("views", __dirname + "/views"); | ||
32 | -app.set("view engine", "ejs"); | ||
33 | - | ||
34 | -app.get("/", function (req, res) { | ||
35 | - request.get(requestUrl, (err, res, body) => { | ||
36 | - if (err) { | ||
37 | - console.log("err => " + err); | ||
38 | - } else { | ||
39 | - if (res.statusCode == 200) { | ||
40 | - // Read url success | ||
41 | - var result = body; | ||
42 | - var xmlToJson = convert.xml2json(result, { compact: true, spaces: 4 }); | ||
43 | - console.log(result); | ||
44 | - console.log(xmlToJson); | ||
45 | - fs.writeFileSync("holi.xml", result); // Create/Modify holi.xml | ||
46 | - fs.writeFileSync("holi.json", xmlToJson); // Create/Modify holi.json | ||
47 | - var parser = new xml2js.Parser(); | ||
48 | - parser.parseString(result, function (err, res) { | ||
49 | - console.log(res); | ||
50 | - text = JSON.stringify(res); | ||
51 | - console.log(text); | ||
52 | - // Get dataName method | ||
53 | - dateName = []; | ||
54 | - var idx = text.indexOf("dateName", 0); | ||
55 | - while (idx != -1) { | ||
56 | - console.log(idx); | ||
57 | - var start = text.indexOf("[", idx) + 2; | ||
58 | - var end = text.indexOf("]", idx) - 1; | ||
59 | - var tempStr = text.substring(start, end); | ||
60 | - console.log(tempStr); | ||
61 | - dateName.push(tempStr); | ||
62 | - idx = text.indexOf("dateName", idx + 1); | ||
63 | - } | ||
64 | - console.log(dateName); | ||
65 | - // Get locdate method | ||
66 | - locdate = []; | ||
67 | - idx = text.indexOf("locdate", 0); | ||
68 | - while (idx != -1) { | ||
69 | - console.log(idx); | ||
70 | - var start = text.indexOf("[", idx) + 2; | ||
71 | - var end = text.indexOf("]", idx) - 1; | ||
72 | - var tempStr = text.substring(start, end); | ||
73 | - console.log(tempStr); | ||
74 | - locdate.push(tempStr); | ||
75 | - idx = text.indexOf("locdate", idx + 1); | ||
76 | - } | ||
77 | - console.log(locdate); | ||
78 | - // Create tempArr to save dateName and locdate at once | ||
79 | - tempArr = []; | ||
80 | - tempArr.push(dateName); | ||
81 | - tempArr.push(locdate); | ||
82 | - console.log(tempArr); | ||
83 | - }); | ||
84 | - } | ||
85 | - } | ||
86 | - }); | ||
87 | - // Send data from nodejs to ejs | ||
88 | - res.render("data.ejs", { data: tempArr }, function (err, html) { | ||
89 | - if (err) { | ||
90 | - console.log(err); | ||
91 | - } | ||
92 | - res.end(html); // End response | ||
93 | - }); | ||
94 | - // send data.ejs | ||
95 | - res.sendFile(__dirname + "/views/data.ejs"); | ||
96 | -}); | ||
97 | - | ||
98 | -const port = 8080; | ||
99 | -app.listen(port, () => console.log("Listening on port " + port)); |
views/data.ejs
deleted
100644 → 0
1 | -<!-- data.ejs --> | ||
2 | - | ||
3 | -<!DOCTYPE html> | ||
4 | -<html lang="en"> | ||
5 | -<head> | ||
6 | - <meta charset="UTF-8"> | ||
7 | - <meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
8 | - <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
9 | - <title>Document</title> | ||
10 | -</head> | ||
11 | -<body> | ||
12 | - <% for(var i = 0; i < data[0].length; i++) {%> | ||
13 | - <p> | ||
14 | - <h1><%= data[0][i] %> <%= data[1][i] %></h1> | ||
15 | - </p> | ||
16 | - <% } %> | ||
17 | -</body> | ||
18 | -</html> | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment