{"size": 1421, "ext": "js", "max_stars_repo_path": "server.js", "max_stars_repo_name": "scottybuoy/note-taker", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server.js", "max_issues_repo_name": "scottybuoy/note-taker", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server.js", "max_forks_repo_name": "scottybuoy/note-taker", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 19.2027027027, "max_line_length": 86, "alphanum_fraction": 0.5594651654}
// import
const express = require("express");
const path = require("path");
const fs = require("fs");
const uniqueId = require("./helpers/uniqueId.js");
const { addNewNote } = require("./helpers/addNewNote.js");

// create variable for port
const PORT = process.env.PORT || 3001;

// create instance of express
const app = express();

// middleware
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static("public"));




// ---------- get routes ------------

// app.get("/", (req, res) => res.send("Supppppp"));

app.get("/notes", (req, res) => {
    res.sendFile(path.join(__dirname, "public/notes.html"));
});

app.get("/api/notes", (req, res) => {
    fs.readFile("./db/db.json", "utf8", (err, data) => {
        if (err) {
            console.error(err);
        } else {
            res.json(JSON.parse(data));
        }
    });
});

app.get("*", (req, res) => res.sendFile(path.join(__dirname, "./public/index.html")));




// -------- post route ------------

app.post("/api/notes", (req, res) => {
    
    const { title, text } = req.body;

    const newNote = {
        title,
        text,
        id: uniqueId()
    };


    // read from db.json, add new note data, then overwrite

    addNewNote(newNote, "./db/db.json");

});







// initiate simulated server
app.listen(PORT, () => {
    console.log(`Example app listening at http://localhost:${PORT}`);
  });
  