-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.js
More file actions
94 lines (77 loc) · 2.76 KB
/
Copy pathServer.js
File metadata and controls
94 lines (77 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Server.js — InfoWiki (Wikipedia REST API only)
const express = require("express");
const path = require("path");
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json({ limit: "10mb" }));
// CORS (dev)
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", "Content-Type, Authorization");
if (req.method === "OPTIONS") return res.sendStatus(200);
next();
});
app.use(express.static(path.join(__dirname, "Frontend")));
/* UTILS */
function stripHtml(html) {
if (!html) return "";
return html.replace(/<[^>]*>?/gm, " ").replace(/\s+/g, " ").trim();
}
function pick(obj, p, fallback = "") {
try {
return p.split(".").reduce((acc, k) => acc?.[k], obj) ?? fallback;
} catch {
return fallback;
}
}
/* WIKIPEDIA REST API */
async function fetchWikiFull(title, lang = "fr") {
const host = `https://${lang}.wikipedia.org`;
const summaryRes = await fetch(
`${host}/api/rest_v1/page/summary/${encodeURIComponent(title)}`
);
if (!summaryRes.ok) {
throw new Error("Page introuvable");
}
const summary = await summaryRes.json();
const htmlRes = await fetch(
`${host}/api/rest_v1/page/html/${encodeURIComponent(title)}`
);
let html = htmlRes.ok ? await htmlRes.text() : "";
html = html
.replace(/<link\b[^>]*>/gm, "")
.replace(/<style\b[^>]*>([\s\S]*?)<\/style>/gm, "");
return {
title: summary.title,
description: summary.description,
imageUrl: summary.originalimage?.source || summary.thumbnail?.source,
html,
fullContent: stripHtml(html),
url: pick(summary, "content_urls.desktop.page", ""),
lang
};
}
/* ROUTE: SEARCH */
app.post("/api/search", async (req, res) => {
try {
const { query, lang } = req.body || {};
if (!query) return res.status(400).json({ message: "query manquant" });
const data = await fetchWikiFull(query, lang || "fr");
return res.json(data);
} catch (e) {
return res.status(404).json({ message: e.message || "Erreur Wikipedia" });
}
});
/* HEALTH */
app.get("/api/health", (req, res) => {
res.json({ status: "ok", timestamp: new Date().toISOString() });
});
/* START */
app.listen(PORT, () => {
console.log("═══════════════════════════════════════════════");
console.log(`🚀 InfoWiki sur http://localhost:${PORT}`);
console.log("✅ Wikipedia REST API activée");
console.log("✅ Puter AI côté Frontend (gpt-5-nano)");
console.log("═══════════════════════════════════════════════");
});