const express = require("express")
const app = express()
const path = require("path")
const fs = require("node:fs")


app.set("view engine", "ejs")
let initialPath = path.join(__dirname, "public")
app.use(express.static(initialPath));
app.use(express.json())


app.get("/", (req, res) => {
    res.render("index")
})

app.post("/feltoltes", (req, res) => {
    const pendrive = "F:\\Suli\\13.b\\Frontend\\Javascript\\2024. 11. 18\\public\\json\\munkavallalok.json"
    const gep = "D:\\Kingston_Pendrive\\Suli\\13.b\\Frontend\\Javascript\\2024. 11. 18\\public\\json\\munkavallalok.json"

    fs.readFile(gep, "utf8", (err, data) => {   
        let adat = JSON.parse(data) 
        adat.push(req.body)
        
        fs.writeFile(gep, JSON.stringify(adat), (err, data) => {
            if (err) {
                console.log(err);
                return
            }
            else {
                console.log("Sikeres feltöltés!");
            }
        })

        res.status(201).send(JSON.stringify({data: adat}))
    })
})

app.listen(3000)