diff --git a/js/server.js b/js/server.js index 3e48323d..b4d71ca5 100644 --- a/js/server.js +++ b/js/server.js @@ -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); }); diff --git a/modules/default/weather/providers/envcanada.js b/modules/default/weather/providers/envcanada.js index 868ee6e2..9ea65e2f 100644 --- a/modules/default/weather/providers/envcanada.js +++ b/modules/default/weather/providers/envcanada.js @@ -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"; }, //