first cors approach

This commit is contained in:
Karsten Hassel 2022-01-24 23:45:17 +01:00
parent d181365620
commit 7cfc7b9d74
2 changed files with 22 additions and 1 deletions

View File

@ -10,6 +10,7 @@ const path = require("path");
const ipfilter = require("express-ipfilter").IpFilter;
const fs = require("fs");
const helmet = require("helmet");
const fetch = require("node-fetch");
const Log = require("logger");
const Utils = require("./utils.js");
@ -61,6 +62,7 @@ function Server(config, callback) {
app.use(function (req, res, next) {
ipfilter(config.ipWhitelist, { mode: config.ipWhitelist.length === 0 ? "deny" : "allow", log: false })(req, res, function (err) {
if (err === undefined) {
res.header("Access-Control-Allow-Origin", "*");
return next();
}
Log.log(err.message);
@ -76,6 +78,25 @@ 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);
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);
});
});
app.get("/version", function (req, res) {
res.send(global.version);
});

View File

@ -164,7 +164,7 @@ WeatherProvider.register("envcanada", {
// CORS errors when accessing EC
//
getUrl() {
return "https://thingproxy.freeboard.io/fetch/https://dd.weather.gc.ca/citypage_weather/xml/" + this.config.provCode + "/" + this.config.siteCode + "_e.xml";
return "http://localhost:8080/cors?url=https://dd.weather.gc.ca/citypage_weather/xml/" + this.config.provCode + "/" + this.config.siteCode + "_e.xml";
},
//