working version, use corsUrl in weather providers

This commit is contained in:
Karsten Hassel 2022-01-25 22:30:16 +01:00
parent 7cfc7b9d74
commit c622db918b
10 changed files with 42 additions and 25 deletions

View file

@ -78,23 +78,31 @@ function Server(config, callback) {
app.use(directory, express.static(path.resolve(global.root_path + directory)));
}
app.get("/cors", function (req, res) {
// http://localhost:8080/cors?url=https://google.de
console.dir(req.query.url);
app.get("/cors", async function (req, res) {
// example: http://localhost:8080/cors?url=https://google.de
fetch(req.query.url, { headers: { "User-Agent": "Mozilla/5.0 MagicMirror/" + global.version } })
.then((response) => {
global.mmCorsHeader = response.headers.get("Content-Type");
if (! global.mmCorsHeader) {global.mmCorsHeader = "application/text"}
return response.text();
})
.then((responseData) => {
res.set("Content-Type", global.mmCorsHeader);
res.send(responseData);
})
.catch((error) => {
Log.error(error);
});
try {
const reg = "^/cors.+url=(.*)";
let url = "";
let match = new RegExp(reg, "g").exec(req.url);
if (!match) {
url = "invalid url: " + req.url;
Log.error(url);
res.send(url);
} else {
url = match[1];
Log.log("cors url: " + url);
const response = await fetch(url, { headers: { "User-Agent": "Mozilla/5.0 MagicMirror/" + global.version } });
const header = response.headers.get("Content-Type");
const data = await response.text();
if (header) res.set("Content-Type", header);
res.send(data);
}
} catch (error) {
Log.error(error);
res.send(error);
}
});
app.get("/version", function (req, res) {