42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
import express from "express";
|
||
import fs from "fs";
|
||
import path from "path";
|
||
|
||
const app = express();
|
||
const port = 4173;
|
||
|
||
// API endpointleri
|
||
app.post("/frontend-api/data", express.json(), (req, res) => {
|
||
fs.writeFile(path.join(process.cwd(), "dist", "data.json"), JSON.stringify(req.body, null, 2), (err) => {
|
||
if (err) {
|
||
res.status(500).send(err.message);
|
||
} else {
|
||
res.status(200).json({ ok: true });
|
||
}
|
||
});
|
||
});
|
||
|
||
app.get("/frontend-api/data", (req, res) => {
|
||
fs.readFile(path.join(process.cwd(), "dist", "data.json"), "utf-8", (err, data) => {
|
||
if (err) {
|
||
res.status(500).send(err.message);
|
||
} else {
|
||
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||
res.setHeader("Pragma", "no-cache");
|
||
res.setHeader("Expires", "0");
|
||
res.setHeader("Content-Type", "application/json");
|
||
res.send(data);
|
||
}
|
||
});
|
||
});
|
||
|
||
// Statik dosyaları sun ve diğer tüm istekleri index.html'e yönlendir
|
||
app.use(express.static(path.join(process.cwd(), "dist")));
|
||
app.get(/^(?!\/api).*/, (req, res) => {
|
||
res.sendFile(path.join(process.cwd(), "dist", "index.html"));
|
||
});
|
||
|
||
app.listen(port, () => {
|
||
console.log(`Sunucu http://localhost:${port} adresinde çalışıyor`);
|
||
});
|