Update server.js
Browse files
server.js
CHANGED
|
@@ -3,7 +3,7 @@ import fetch from "node-fetch";
|
|
| 3 |
import dotenv from "dotenv";
|
| 4 |
dotenv.config();
|
| 5 |
|
| 6 |
-
const app
|
| 7 |
const PORT = process.env.PORT || 7860;
|
| 8 |
|
| 9 |
if (!process.env.OPENAI_KEY) {
|
|
@@ -12,17 +12,21 @@ if (!process.env.OPENAI_KEY) {
|
|
| 12 |
}
|
| 13 |
|
| 14 |
app.use(express.json());
|
|
|
|
|
|
|
| 15 |
app.use(express.static("public"));
|
| 16 |
|
|
|
|
| 17 |
// —— Angol KJV vers lekérése ——
|
| 18 |
async function fetchEnglishVerse(book, ch, v) {
|
| 19 |
-
|
| 20 |
-
const url = "https://bible-api.com/" +
|
| 21 |
-
encodeURIComponent(book + " " + ch + ":
|
| 22 |
"?translation=kjv";
|
| 23 |
-
|
|
|
|
| 24 |
if (!r.ok) throw new Error("bible-api hiba");
|
| 25 |
-
const j
|
| 26 |
return j.text.trim();
|
| 27 |
}
|
| 28 |
|
|
@@ -32,7 +36,7 @@ app.post("/api/analyze", async (req, res) => {
|
|
| 32 |
const { book, chapter, verse } = req.body || {};
|
| 33 |
try {
|
| 34 |
const engVerse = await fetchEnglishVerse(book, chapter, verse);
|
| 35 |
-
const ref
|
| 36 |
|
| 37 |
const aiRes = await fetch("https://api.openai.com/v1/chat/completions", {
|
| 38 |
method: "POST",
|
|
@@ -63,35 +67,39 @@ app.post("/api/analyze", async (req, res) => {
|
|
| 63 |
|
| 64 |
res.json({
|
| 65 |
reference: ref + " (KJV ⇒ HU)",
|
| 66 |
-
english:
|
| 67 |
-
output
|
| 68 |
});
|
| 69 |
} catch (err) {
|
| 70 |
console.error(err);
|
| 71 |
res.status(500).json({ error: err.message || "Ismeretlen hiba" });
|
| 72 |
}
|
| 73 |
});
|
| 74 |
-
|
|
|
|
| 75 |
app.get("/api/meta", async (req, res) => {
|
| 76 |
const book = req.query.book;
|
| 77 |
if (!book) return res.status(400).json({ error: "Missing book" });
|
| 78 |
|
| 79 |
try {
|
| 80 |
const chapters = [];
|
| 81 |
-
for (let ch = 1; ch <= 150; ch++) {
|
|
|
|
| 82 |
const url = "https://bible-api.com/" +
|
| 83 |
-
encodeURIComponent(book + " " + ch
|
| 84 |
"?translation=kjv";
|
| 85 |
const r = await fetch(url);
|
| 86 |
-
if (!r.ok) break;
|
|
|
|
| 87 |
const d = await r.json();
|
| 88 |
-
|
| 89 |
-
|
|
|
|
| 90 |
}
|
| 91 |
-
res.json({ chapters });
|
| 92 |
} catch (err) {
|
| 93 |
res.status(500).json({ error: "Meta fetch error" });
|
| 94 |
}
|
| 95 |
});
|
| 96 |
|
| 97 |
-
app.listen(PORT, () => console.log("🚀 Fut a", PORT, "porton"));
|
|
|
|
| 3 |
import dotenv from "dotenv";
|
| 4 |
dotenv.config();
|
| 5 |
|
| 6 |
+
const app = express();
|
| 7 |
const PORT = process.env.PORT || 7860;
|
| 8 |
|
| 9 |
if (!process.env.OPENAI_KEY) {
|
|
|
|
| 12 |
}
|
| 13 |
|
| 14 |
app.use(express.json());
|
| 15 |
+
// A HTML, CSS, JS fájlokhoz kell egy "public" mappa
|
| 16 |
+
// Hozd létre a "public" mappát és helyezd bele a HTML fájlodat (pl. index.html néven)
|
| 17 |
app.use(express.static("public"));
|
| 18 |
|
| 19 |
+
|
| 20 |
// —— Angol KJV vers lekérése ——
|
| 21 |
async function fetchEnglishVerse(book, ch, v) {
|
| 22 |
+
// JAVÍTÁS: A ":1" helyett a kapott versszámot (`v`) használjuk
|
| 23 |
+
const url = "https://bible-api.com/" +
|
| 24 |
+
encodeURIComponent(book + " " + ch + ":" + v) +
|
| 25 |
"?translation=kjv";
|
| 26 |
+
|
| 27 |
+
const r = await fetch(url);
|
| 28 |
if (!r.ok) throw new Error("bible-api hiba");
|
| 29 |
+
const j = await r.json();
|
| 30 |
return j.text.trim();
|
| 31 |
}
|
| 32 |
|
|
|
|
| 36 |
const { book, chapter, verse } = req.body || {};
|
| 37 |
try {
|
| 38 |
const engVerse = await fetchEnglishVerse(book, chapter, verse);
|
| 39 |
+
const ref = book + " " + chapter + ":" + verse;
|
| 40 |
|
| 41 |
const aiRes = await fetch("https://api.openai.com/v1/chat/completions", {
|
| 42 |
method: "POST",
|
|
|
|
| 67 |
|
| 68 |
res.json({
|
| 69 |
reference: ref + " (KJV ⇒ HU)",
|
| 70 |
+
english: engVerse,
|
| 71 |
+
output // tartalmaz: HU vers + kommentár
|
| 72 |
});
|
| 73 |
} catch (err) {
|
| 74 |
console.error(err);
|
| 75 |
res.status(500).json({ error: err.message || "Ismeretlen hiba" });
|
| 76 |
}
|
| 77 |
});
|
| 78 |
+
|
| 79 |
+
// ------------- JAVÍTOTT /api/meta -------------
|
| 80 |
app.get("/api/meta", async (req, res) => {
|
| 81 |
const book = req.query.book;
|
| 82 |
if (!book) return res.status(400).json({ error: "Missing book" });
|
| 83 |
|
| 84 |
try {
|
| 85 |
const chapters = [];
|
| 86 |
+
for (let ch = 1; ch <= 150; ch++) { // 150 elég nagy szám (Zsoltárok miatt)
|
| 87 |
+
// JAVÍTÁS: Az egész fejezetet kérjük le, nem csak az első verset (":1" eltávolítva)
|
| 88 |
const url = "https://bible-api.com/" +
|
| 89 |
+
encodeURIComponent(book + " " + ch) +
|
| 90 |
"?translation=kjv";
|
| 91 |
const r = await fetch(url);
|
| 92 |
+
if (!r.ok) break; // elfogyott a fejezet, kilépünk a ciklusból
|
| 93 |
+
|
| 94 |
const d = await r.json();
|
| 95 |
+
// Az utolsó vers sorszáma adja meg a fejezet hosszát
|
| 96 |
+
const lastVerseInChapter = d.verses[d.verses.length - 1].verse;
|
| 97 |
+
chapters.push(lastVerseInChapter);
|
| 98 |
}
|
| 99 |
+
res.json({ chapters }); // pl. [31,25,24,…]
|
| 100 |
} catch (err) {
|
| 101 |
res.status(500).json({ error: "Meta fetch error" });
|
| 102 |
}
|
| 103 |
});
|
| 104 |
|
| 105 |
+
app.listen(PORT, () => console.log("🚀 Fut a", PORT, "porton"));
|