-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
48 lines (46 loc) · 1.7 KB
/
server.js
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
const express = require("express");
const app = express();
app.set("json spaces", 2);
const fetch = require("node-fetch");
const cheerio = require("cheerio");
app.use((req, res) => {
res.header("Access-Control-Allow-Origin", "*");
if (!req.query.url)
return res.json({
error: "An error has occured, you may have inputted an incorrect url.",
usage: `https://${req.hostname}/?url=https://google.com`
});
fetch(req.query.url)
.then((result) => result.text())
.then((page) => {
const $ = cheerio.load(page);
var title =
$('meta[property="og:title"]').attr("content") ||
$("title").text() ||
$('meta[name="title"]').attr("content");
var description =
$('meta[property="og:description"]').attr("content") ||
$('meta[name="description"]').attr("content");
var url = $('meta[property="og:url"]').attr("content");
var site_name = $('meta[property="og:site_name"]').attr("content");
var image =
$('meta[property="og:image"]').attr("content") ||
$('meta[property="og:image:url"]').attr("content");
var icon =
$('link[rel="icon"]').attr("href") ||
$('link[rel="shortcut icon"]').attr("href");
var keywords =
$('meta[property="og:keywords"]').attr("content") ||
$('meta[name="keywords"]').attr("content");
res.json({ title, description, url, site_name, image, icon, keywords });
})
.catch((err) => {
return res.json({
error: "An error has occured, you may have inputted an incorrect url.",
usage: `https://${req.hostname}/?url=https://google.com`
});
});
});
const listener = app.listen(3000, () => {
console.log("Server started!");
});