server.js 2.93 KB
const express = require("express");
const app = express();
const request = require("request");
const convert = require("xml-js");
const fs = require("fs");
const xml2js = require("xml2js");

// http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/getHoliDeInfo?solYear=2019&solMonth=03&ServiceKey=서비스키

var SERVEICE_KEY =
  "qBtJy2Prw8CCnAiijUM7VkuaA9MZozHuiQI4FbEGYdUDPz4%2FM%2FuxegGjNBWK0aWQHvSslVHwIZQwNWh57WgRTA%3D%3D";
var operation = "getHoliDeInfo";
var url =
  "http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService/" +
  operation;
var queryParams = "?" + "solYear" + "=" + "2022";
queryParams += "&" + "solMonth" + "=" + "09";
queryParams += "&" + "ServiceKey" + "=" + SERVEICE_KEY;
let requestUrl = url + queryParams;
var text = "";
var text2 = "";
var dateName = [];
var locdate = [];
var tempArr = [];

// To run EJS engine
app.set("views", __dirname + "/views");
app.set("view engine", "ejs");

app.get("/", function (req, res) {
  request.get(requestUrl, (err, res, body) => {
    if (err) {
      console.log("err => " + err);
    } else {
      if (res.statusCode == 200) {
        var result = body;
        var xmlToJson = convert.xml2json(result, { compact: true, spaces: 4 });
        console.log(result);
        console.log(xmlToJson);
        fs.writeFileSync("holi.json", xmlToJson);
        fs.writeFileSync("holi.xml", result);
        var parser = new xml2js.Parser();
        parser.parseString(result, function (err, res) {
          console.log(res);
          text = JSON.stringify(res);
          console.log(text);
          dateName = [];
          var idx = text.indexOf("dateName", 0);
          while (idx != -1) {
            console.log(idx);
            var start = text.indexOf("[", idx) + 2;
            var end = text.indexOf("]", idx) - 1;
            var tempStr = text.substring(start, end);
            console.log(tempStr);
            dateName.push(tempStr);
            idx = text.indexOf("dateName", idx + 1);
          }
          console.log(dateName);
          locdate = [];
          idx = text.indexOf("locdate", 0);
          while (idx != -1) {
            console.log(idx);
            var start = text.indexOf("[", idx) + 2;
            var end = text.indexOf("]", idx) - 1;
            var tempStr = text.substring(start, end);
            console.log(tempStr);
            locdate.push(tempStr);
            idx = text.indexOf("locdate", idx + 1);
          }
          console.log(locdate);
          text2 = dateName.toString() + "&" + locdate.toString();
          tempArr = [];
          tempArr.push(dateName);
          tempArr.push(locdate);
          console.log(tempArr);
        });
      }
    }
  });
  res.render("data.ejs", { data: tempArr }, function (err, html) {
    if (err) {
      console.log(err);
    }
    res.end(html); // 응답 종료
  });
  res.sendFile(__dirname + "/views/data.ejs");
});

const port = 8080;
app.listen(port, () => console.log("Listening on port " + port));