박지환

Add comments

Showing 1 changed file with 18 additions and 9 deletions
...@@ -5,20 +5,24 @@ const convert = require("xml-js"); ...@@ -5,20 +5,24 @@ const convert = require("xml-js");
5 const fs = require("fs"); 5 const fs = require("fs");
6 const xml2js = require("xml2js"); 6 const xml2js = require("xml2js");
7 7
8 -// http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getHoliDeInfo?solYear=2019&solMonth=03&ServiceKey=서비스키 8 +// Modify the values as needed
9 +var year = "2022";
10 +var month = "09";
11 +var operation = "getHoliDeInfo";
9 12
13 +// Do not modify the values
10 var SERVEICE_KEY = 14 var SERVEICE_KEY =
11 "qBtJy2Prw8CCnAiijUM7VkuaA9MZozHuiQI4FbEGYdUDPz4%2FM%2FuxegGjNBWK0aWQHvSslVHwIZQwNWh57WgRTA%3D%3D"; 15 "qBtJy2Prw8CCnAiijUM7VkuaA9MZozHuiQI4FbEGYdUDPz4%2FM%2FuxegGjNBWK0aWQHvSslVHwIZQwNWh57WgRTA%3D%3D";
12 -var operation = "getHoliDeInfo";
13 var url = 16 var url =
14 "http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/" + 17 "http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/" +
15 operation; 18 operation;
16 -var queryParams = "?" + "solYear" + "=" + "2022"; 19 +var queryParams = "?" + "solYear" + "=" + year;
17 -queryParams += "&" + "solMonth" + "=" + "09"; 20 +queryParams += "&" + "solMonth" + "=" + month;
18 queryParams += "&" + "ServiceKey" + "=" + SERVEICE_KEY; 21 queryParams += "&" + "ServiceKey" + "=" + SERVEICE_KEY;
19 let requestUrl = url + queryParams; 22 let requestUrl = url + queryParams;
23 +
24 +// Empty variables
20 var text = ""; 25 var text = "";
21 -var text2 = "";
22 var dateName = []; 26 var dateName = [];
23 var locdate = []; 27 var locdate = [];
24 var tempArr = []; 28 var tempArr = [];
...@@ -33,17 +37,19 @@ app.get("/", function (req, res) { ...@@ -33,17 +37,19 @@ app.get("/", function (req, res) {
33 console.log("err => " + err); 37 console.log("err => " + err);
34 } else { 38 } else {
35 if (res.statusCode == 200) { 39 if (res.statusCode == 200) {
40 + // Read url success
36 var result = body; 41 var result = body;
37 var xmlToJson = convert.xml2json(result, { compact: true, spaces: 4 }); 42 var xmlToJson = convert.xml2json(result, { compact: true, spaces: 4 });
38 console.log(result); 43 console.log(result);
39 console.log(xmlToJson); 44 console.log(xmlToJson);
40 - fs.writeFileSync("holi.json", xmlToJson); 45 + fs.writeFileSync("holi.xml", result); // Create/Modify holi.xml
41 - fs.writeFileSync("holi.xml", result); 46 + fs.writeFileSync("holi.json", xmlToJson); // Create/Modify holi.json
42 var parser = new xml2js.Parser(); 47 var parser = new xml2js.Parser();
43 parser.parseString(result, function (err, res) { 48 parser.parseString(result, function (err, res) {
44 console.log(res); 49 console.log(res);
45 text = JSON.stringify(res); 50 text = JSON.stringify(res);
46 console.log(text); 51 console.log(text);
52 + // Get dataName method
47 dateName = []; 53 dateName = [];
48 var idx = text.indexOf("dateName", 0); 54 var idx = text.indexOf("dateName", 0);
49 while (idx != -1) { 55 while (idx != -1) {
...@@ -56,6 +62,7 @@ app.get("/", function (req, res) { ...@@ -56,6 +62,7 @@ app.get("/", function (req, res) {
56 idx = text.indexOf("dateName", idx + 1); 62 idx = text.indexOf("dateName", idx + 1);
57 } 63 }
58 console.log(dateName); 64 console.log(dateName);
65 + // Get locdate method
59 locdate = []; 66 locdate = [];
60 idx = text.indexOf("locdate", 0); 67 idx = text.indexOf("locdate", 0);
61 while (idx != -1) { 68 while (idx != -1) {
...@@ -68,7 +75,7 @@ app.get("/", function (req, res) { ...@@ -68,7 +75,7 @@ app.get("/", function (req, res) {
68 idx = text.indexOf("locdate", idx + 1); 75 idx = text.indexOf("locdate", idx + 1);
69 } 76 }
70 console.log(locdate); 77 console.log(locdate);
71 - text2 = dateName.toString() + "&" + locdate.toString(); 78 + // Create tempArr to save dateName and locdate at once
72 tempArr = []; 79 tempArr = [];
73 tempArr.push(dateName); 80 tempArr.push(dateName);
74 tempArr.push(locdate); 81 tempArr.push(locdate);
...@@ -77,12 +84,14 @@ app.get("/", function (req, res) { ...@@ -77,12 +84,14 @@ app.get("/", function (req, res) {
77 } 84 }
78 } 85 }
79 }); 86 });
87 + // Send data from nodejs to ejs
80 res.render("data.ejs", { data: tempArr }, function (err, html) { 88 res.render("data.ejs", { data: tempArr }, function (err, html) {
81 if (err) { 89 if (err) {
82 console.log(err); 90 console.log(err);
83 } 91 }
84 - res.end(html); // 응답 종료 92 + res.end(html); // End response
85 }); 93 });
94 + // send data.ejs
86 res.sendFile(__dirname + "/views/data.ejs"); 95 res.sendFile(__dirname + "/views/data.ejs");
87 }); 96 });
88 97
......