forked from ShadowDevLabs/fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (60 loc) · 1.87 KB
/
index.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { ChemicalServer } from "chemicaljs";
import { fileURLToPath } from "url";
import { dirname } from "path";
import express from "express";
import compression from "compression";
import cheerio from "cheerio";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const chemical = new ChemicalServer();
const port = process.env.PORT || 8080;
const version = process.env.npm_package_version;
const publicPath = fileURLToPath(new URL("./public/", import.meta.url));
chemical.use(compression());
chemical.app.use(
express.static(publicPath, {
extensions: ["html"],
maxAge: 604800000,
})
);
chemical.app.get("/version", (req, res) => {
if (req.query.v && req.query.v != version) {
res.status(400).send(version);
return;
}
res.status(200).send(version);
});
chemical.app.get("/get-title", async (req, res) => {
const { url } = req.query;
try {
const response = await fetch(url);
const html = await response.text();
const title =
html.match(/<title>(.*?)<\/title>/i)?.[1] || "Title not found";
res.send({ title });
} catch {
res.status(500).send("Error fetching the URL");
}
});
chemical.use("/privacy", express.static("public" + "/privacy.html"));
chemical.app.get("/search-api", async (req, res) => {
const response = await fetch(
`http://api.duckduckgo.com/ac?q=${req.query.term}&format=json`
).then((i) => i.json());
res.send(response);
});
chemical.app.get("/user-agents", async (req, res) => {
let text = await fetch("https://useragents.me/");
text = await text.text();
const $ = cheerio.load(text);
res.send(
$("#most-common-desktop-useragents-json-csv > div:eq(0) > textarea").val()
);
});
chemical.error((req, res) => {
res.status(404);
res.sendFile(`${__dirname}/public/404.html`);
});
chemical.listen(port, () => {
console.log(`Fluid listening on port ${port}`);
});