mirror of
https://codeberg.org/ashley/poke.git
synced 2025-06-08 19:53:01 -04:00
Update 'p/server.js'
This commit is contained in:
parent
b32bf35896
commit
f6abeaa97b
1 changed files with 13 additions and 1 deletions
60
p/server.js
Normal file
60
p/server.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
const fs = require("fs");
|
||||
const express = require("express");
|
||||
const fetch = require("node-fetch");
|
||||
const htmlParser = require("node-html-parser");
|
||||
const lyrics = require("./lyrics.js");
|
||||
|
||||
const app = express();
|
||||
|
||||
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);
|
||||
|
||||
let f = await fetch(url, {
|
||||
method: req.method,
|
||||
});
|
||||
if (false && f.headers.get("content-type").includes("html")) {
|
||||
const body = await f.text();
|
||||
if (false && !htmlParser.valid(body)) {
|
||||
console.warn(`[ERROR] Invalid HTML at ${url}`);
|
||||
f.body.pipe(res);
|
||||
return;
|
||||
}
|
||||
const root = htmlParser.parse(body);
|
||||
let html = root.childNodes.filter(
|
||||
(x) => x.tagName && x.tagName.toLowerCase() == "html"
|
||||
)[0];
|
||||
|
||||
if (!html) {
|
||||
console.warn(`[ERROR] No <html> at ${url}`);
|
||||
res.send(body);
|
||||
return;
|
||||
}
|
||||
|
||||
res.send(html.toString());
|
||||
} else {
|
||||
f.body.pipe(res);
|
||||
}
|
||||
};
|
||||
|
||||
const listener = (req, res) => {
|
||||
Proxy(req, res);
|
||||
};
|
||||
|
||||
app.get("/", (req, res) => res.redirect(`/https://www.google.com/`));
|
||||
|
||||
|
||||
app.get("/api/lyrics", async (req, res) => {
|
||||
const query = req.query.query;
|
||||
|
||||
res.json(await lyrics(query))
|
||||
|
||||
});
|
||||
|
||||
|
||||
app.all("/*", listener);
|
||||
|
||||
app.listen(3000, () => {});
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue