const express = require("express")
const path = require("path")
const fs = require("node:fs")
const initialPath = path.join(__dirname, "public")
const app = express()


app.set("view engine", "ejs")
app.use(express.static(initialPath))
app.use(express.json())


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

app.post("/feltoltes", (req, res) => {
    fs.readFile("receptek.json", "utf8", (err, data) => {
        if (err) {
            console.log("Hiba történt: ", err);
            return;
        }
        
        const receptek = JSON.parse(data)
        receptek.push(req.body)

        fs.writeFile("receptek.json", JSON.stringify(receptek), (err, data) => {
            if (err) {
                console.log("Hiba történt: ", err);
                return;
            }
        })

        res.status(200).json(receptek)
    })
})


app.listen(3000)