-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
122 lines (106 loc) · 3.33 KB
/
server.js
File metadata and controls
122 lines (106 loc) · 3.33 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const express = require("express");
const cheerio = require("cheerio");
const path = require("path");
const app = express();
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
app.post("/api/fetch-og", async (req, res) => {
const { url } = req.body;
if (!url) {
return res.status(400).json({ error: "URL is required" });
}
// Validate URL format
try {
const parsed = new URL(url);
if (!["http:", "https:"].includes(parsed.protocol)) {
return res.status(400).json({ error: "Only HTTP/HTTPS URLs are allowed" });
}
} catch {
return res.status(400).json({ error: "Invalid URL" });
}
try {
const response = await fetch(url, {
headers: {
"User-Agent":
"Mozilla/5.0 (compatible; OpenGraphViewer/1.0; +http://localhost)",
},
signal: AbortSignal.timeout(5000),
});
const html = await response.text();
const $ = cheerio.load(html);
const meta = {};
// Collect all meta tags (og:, twitter:, standard)
$("meta").each((_, el) => {
const property =
$(el).attr("property") || $(el).attr("name") || "";
const content = $(el).attr("content") || "";
if (property && content) {
meta[property.toLowerCase()] = content;
}
});
const ogData = {
title:
meta["og:title"] ||
meta["twitter:title"] ||
$("title").text() ||
"",
description:
meta["og:description"] ||
meta["twitter:description"] ||
meta["description"] ||
"",
image:
meta["og:image"] || meta["twitter:image"] || "",
imageWidth: meta["og:image:width"] || "",
imageHeight: meta["og:image:height"] || "",
imageAlt:
meta["og:image:alt"] || meta["twitter:image:alt"] || "",
url: meta["og:url"] || url,
siteName: meta["og:site_name"] || "",
type: meta["og:type"] || "",
twitterCard: meta["twitter:card"] || "",
twitterSite: meta["twitter:site"] || "",
twitterCreator: meta["twitter:creator"] || "",
locale: meta["og:locale"] || "",
themeColor: meta["theme-color"] || "",
favicon:
$('link[rel="icon"]').attr("href") ||
$('link[rel="shortcut icon"]').attr("href") ||
"",
allMeta: meta,
};
// Resolve relative image/favicon URLs
if (ogData.image && !ogData.image.startsWith("http")) {
ogData.image = new URL(ogData.image, url).href;
}
if (ogData.favicon && !ogData.favicon.startsWith("http")) {
ogData.favicon = new URL(ogData.favicon, url).href;
}
res.json(ogData);
} catch (err) {
res.status(500).json({
error: `Failed to fetch URL: ${err.message}`,
});
}
});
function startServer(port = 0) {
return new Promise((resolve, reject) => {
const server = app.listen(port, () => {
const actualPort = server.address().port;
console.log(`OpenGraph Viewer running at http://localhost:${actualPort}`);
resolve({ server, port: actualPort });
});
server.on('error', (err) => {
reject(err);
});
});
}
// If running directly (not via Electron), start the server on default port
if (require.main === module) {
const PORT = 3456;
startServer(PORT).catch((err) => {
console.error('Failed to start server:', err);
process.exit(1);
});
}
module.exports = { app, startServer };