mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 19:53:36 +00:00
first cors approach
This commit is contained in:
parent
d181365620
commit
7cfc7b9d74
21
js/server.js
21
js/server.js
@ -10,6 +10,7 @@ const path = require("path");
|
|||||||
const ipfilter = require("express-ipfilter").IpFilter;
|
const ipfilter = require("express-ipfilter").IpFilter;
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const helmet = require("helmet");
|
const helmet = require("helmet");
|
||||||
|
const fetch = require("node-fetch");
|
||||||
|
|
||||||
const Log = require("logger");
|
const Log = require("logger");
|
||||||
const Utils = require("./utils.js");
|
const Utils = require("./utils.js");
|
||||||
@ -61,6 +62,7 @@ function Server(config, callback) {
|
|||||||
app.use(function (req, res, next) {
|
app.use(function (req, res, next) {
|
||||||
ipfilter(config.ipWhitelist, { mode: config.ipWhitelist.length === 0 ? "deny" : "allow", log: false })(req, res, function (err) {
|
ipfilter(config.ipWhitelist, { mode: config.ipWhitelist.length === 0 ? "deny" : "allow", log: false })(req, res, function (err) {
|
||||||
if (err === undefined) {
|
if (err === undefined) {
|
||||||
|
res.header("Access-Control-Allow-Origin", "*");
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
Log.log(err.message);
|
Log.log(err.message);
|
||||||
@ -76,6 +78,25 @@ function Server(config, callback) {
|
|||||||
app.use(directory, express.static(path.resolve(global.root_path + directory)));
|
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) {
|
app.get("/version", function (req, res) {
|
||||||
res.send(global.version);
|
res.send(global.version);
|
||||||
});
|
});
|
||||||
|
@ -164,7 +164,7 @@ WeatherProvider.register("envcanada", {
|
|||||||
// CORS errors when accessing EC
|
// CORS errors when accessing EC
|
||||||
//
|
//
|
||||||
getUrl() {
|
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";
|
||||||
},
|
},
|
||||||
|
|
||||||
//
|
//
|
||||||
|
Loading…
x
Reference in New Issue
Block a user