const http = require("http");
const fs = require("fs");
const common = require(__dirname + "/common");

const server = http.createServer();

server.on("request", (req, res) => {
  fs.readFile("./index.html", { encoding: "utf8" }, (err, data) => {
    if (err) {
      res.writeHead(500);
      res.end("Error Loading File");
    } else {
      res.writeHead(200, {
        "content-type": "text/html",
      });
      res.end(data.replace("@@@", JSON.stringify(common.getAll())));
    }
  });
});

server.listen(3000, () => {
  console.log("server is running at http://127.0.0.1:3000");
});
