net-helper/components/http-simple.js
2025-01-23 19:31:47 +03:00

46 lines
1.1 KiB
JavaScript

const HTTPServer = require("./_http");
const mime = require("mime");
const { simpleHTTP } = HTTPServer;
function logger (...p) {
return;
console.log("[DEBUG.http.simple]:", ...p);
}
module.exports = class HTTPRedirector extends HTTPServer {
static type () {
return "shttp";
}
constructor (adr, syntaxTree) {
super(adr, syntaxTree);
}
run () {
this.preinit();
const redirectTo = this.syntax.target ?? "https://example.org";
this.app.use("*", async (req, res) => {
logger("originalUrl:>", req.originalUrl);
const currentDomain = req.hostname;
logger("hostname:", currentDomain);
const isNotDomained = !this.syntax.domains[currentDomain];
if (!isNotDomained) {
return this
.callDomain(currentDomain, this.syntax.domains[currentDomain])
.argv(this.syntax.domains[currentDomain].Type.type(), req, res);
}
return simpleHTTP(redirectTo, req, res, !!this.syntax.showDir);
});
this.postinit((err) => {
if (err) {
throw err;
}
console.log(`Simple HTTP server listened at ${
this.adr.address
}:${this.adr.port}`);
});
}
};