박규리

Delete server.js

Showing 1 changed file with 0 additions and 126 deletions
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 -function init() {
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 - let i = 0;
78 - let length = dateName.length;
79 - var holiArr = [];
80 - while (i < length) {
81 - var temptoday = dateName[i];
82 - var tempdate = locdate[i];
83 - var modifiedDate =
84 - tempdate.substr(0, 4) +
85 - "-" +
86 - tempdate.substr(4, 2) +
87 - "-" +
88 - tempdate.substr(6, 2);
89 - var today = new Date("2022-05-13"); //오늘 날짜 입력
90 - var holiday = new Date(modifiedDate); //공휴일 날짜 입력
91 - var diffDate = today.getTime() - holiday.getTime();
92 - var dday = Math.abs(diffDate / (1000 * 3600 * 24));
93 - console.log(temptoday + "까지 " + dday + "일 남았습니다.");
94 - holiArr.push(dday);
95 - i++;
96 - }
97 - console.log(locdate);
98 - // Create tempArr to save dateName and locdate at once
99 - tempArr = [];
100 - tempArr.push(dateName);
101 - tempArr.push(locdate);
102 - tempArr.push(holiArr);
103 - console.log(tempArr);
104 - });
105 - }
106 - }
107 - });
108 -}
109 -
110 -app.get("/", function (req, res) {
111 - init();
112 - // Send data from nodejs to ejs
113 - res.render("data.ejs", { data: tempArr }, function (err, html) {
114 - if (err) {
115 - console.log(err);
116 - }
117 - res.end(html); // End response
118 - });
119 - // send data.ejs
120 - res.sendFile(__dirname + "/views/data.ejs");
121 -});
122 -
123 -init();
124 -
125 -const port = 8080;
126 -app.listen(port, () => console.log("Listening on port " + port));