1
0
Fork 0
mirror of https://codeberg.org/ashley/poke.git synced 2024-11-17 07:14:40 -05:00
poke/p/server.js

62 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-08-15 13:41:52 -04:00
const fs = require("fs");
2022-08-15 04:54:24 -04:00
const express = require("express");
const fetch = require("node-fetch");
2022-08-15 04:27:40 -04:00
const htmlParser = require("node-html-parser");
2022-08-15 05:58:08 -04:00
const lyrics = require("./lyrics.js");
2022-08-15 04:27:40 -04:00
const app = express();
2022-08-15 04:54:24 -04:00
app.use(express.json()); // for parsing application/json
app.use(express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
let Proxy = async (req, res) => {
const url = "https://" + req.originalUrl.slice(10);
2022-08-15 04:27:40 -04:00
let f = await fetch(url, {
method: req.method,
});
2022-08-15 04:54:24 -04:00
if (false && f.headers.get("content-type").includes("html")) {
2022-08-15 04:27:40 -04:00
const body = await f.text();
2022-08-15 04:54:24 -04:00
if (false && !htmlParser.valid(body)) {
2022-08-15 04:27:40 -04:00
console.warn(`[ERROR] Invalid HTML at ${url}`);
f.body.pipe(res);
return;
2022-08-15 04:54:24 -04:00
}
2022-08-15 04:27:40 -04:00
const root = htmlParser.parse(body);
2022-08-15 04:54:24 -04:00
let html = root.childNodes.filter(
(x) => x.tagName && x.tagName.toLowerCase() == "html"
)[0];
if (!html) {
2022-08-15 04:27:40 -04:00
console.warn(`[ERROR] No <html> at ${url}`);
res.send(body);
return;
2022-08-15 04:54:24 -04:00
}
2022-08-15 04:27:40 -04:00
res.send(html.toString());
2022-08-15 04:54:24 -04:00
} else {
2022-08-15 04:27:40 -04:00
f.body.pipe(res);
2022-08-15 04:54:24 -04:00
}
2022-08-15 04:27:40 -04:00
};
const listener = (req, res) => {
2022-08-15 04:54:24 -04:00
Proxy(req, res);
2022-08-15 04:27:40 -04:00
};
2022-08-15 13:41:52 -04:00
app.get("/", (req, res) => res.redirect(`/https://html.duckduckgo.com/html`));
2022-08-15 05:58:08 -04:00
app.get("/api/lyrics", async (req, res) => {
const query = req.query.query;
res.json(await lyrics(query))
});
2022-08-15 04:54:24 -04:00
app.all("/*", listener);
2022-08-15 04:27:40 -04:00
2022-08-15 04:54:24 -04:00
app.listen(3000, () => {});
2022-08-15 05:58:08 -04:00
2022-08-15 13:41:52 -04:00