Add CORS to weather endpoint
This commit is contained in:
20
package-lock.json
generated
20
package-lock.json
generated
@@ -67,6 +67,7 @@
|
|||||||
"resolved": "https://registry.npmjs.org/align-text/-/align-text-0.1.4.tgz",
|
"resolved": "https://registry.npmjs.org/align-text/-/align-text-0.1.4.tgz",
|
||||||
"integrity": "sha1-DNkKVhCT810KmSVsIrcGlDP60Rc=",
|
"integrity": "sha1-DNkKVhCT810KmSVsIrcGlDP60Rc=",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"kind-of": "^3.0.2",
|
"kind-of": "^3.0.2",
|
||||||
"longest": "^1.0.1",
|
"longest": "^1.0.1",
|
||||||
@@ -462,6 +463,15 @@
|
|||||||
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
|
||||||
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
|
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
|
||||||
},
|
},
|
||||||
|
"cors": {
|
||||||
|
"version": "2.8.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
|
||||||
|
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
|
||||||
|
"requires": {
|
||||||
|
"object-assign": "^4",
|
||||||
|
"vary": "^1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"currently-unhandled": {
|
"currently-unhandled": {
|
||||||
"version": "0.4.1",
|
"version": "0.4.1",
|
||||||
"resolved": "https://registry.npmjs.org/currently-unhandled/-/currently-unhandled-0.4.1.tgz",
|
"resolved": "https://registry.npmjs.org/currently-unhandled/-/currently-unhandled-0.4.1.tgz",
|
||||||
@@ -1274,7 +1284,8 @@
|
|||||||
"version": "1.1.6",
|
"version": "1.1.6",
|
||||||
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz",
|
"resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz",
|
||||||
"integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==",
|
"integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==",
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"is-builtin-module": {
|
"is-builtin-module": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
@@ -1453,6 +1464,7 @@
|
|||||||
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz",
|
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz",
|
||||||
"integrity": "sha1-MeohpzS6ubuw8yRm2JOupR5KPGQ=",
|
"integrity": "sha1-MeohpzS6ubuw8yRm2JOupR5KPGQ=",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"is-buffer": "^1.1.5"
|
"is-buffer": "^1.1.5"
|
||||||
}
|
}
|
||||||
@@ -1495,7 +1507,8 @@
|
|||||||
"version": "1.0.1",
|
"version": "1.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/longest/-/longest-1.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/longest/-/longest-1.0.1.tgz",
|
||||||
"integrity": "sha1-MKCy2jj3N3DoKUoNIuZiXtd9AJc=",
|
"integrity": "sha1-MKCy2jj3N3DoKUoNIuZiXtd9AJc=",
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"loud-rejection": {
|
"loud-rejection": {
|
||||||
"version": "1.6.0",
|
"version": "1.6.0",
|
||||||
@@ -5199,7 +5212,8 @@
|
|||||||
"version": "1.6.1",
|
"version": "1.6.1",
|
||||||
"resolved": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz",
|
"resolved": "https://registry.npmjs.org/repeat-string/-/repeat-string-1.6.1.tgz",
|
||||||
"integrity": "sha1-jcrkcOHIirwtYA//Sndihtp15jc=",
|
"integrity": "sha1-jcrkcOHIirwtYA//Sndihtp15jc=",
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"repeating": {
|
"repeating": {
|
||||||
"version": "2.0.1",
|
"version": "2.0.1",
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
"start": "node server"
|
"start": "node server"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^6.0.0",
|
"dotenv": "^6.0.0",
|
||||||
"express": "^4.16.3",
|
"express": "^4.16.3",
|
||||||
"geo-tz": "^4.0.1",
|
"geo-tz": "^4.0.1",
|
||||||
|
|||||||
@@ -76,6 +76,9 @@ function getOWMWeatherData( location, callback ) {
|
|||||||
weather.wind = weather.wind / maxCount;
|
weather.wind = weather.wind / maxCount;
|
||||||
weather.precip = weather.precip / maxCount;
|
weather.precip = weather.precip / maxCount;
|
||||||
weather.icon = data.list[0].weather[0].icon;
|
weather.icon = data.list[0].weather[0].icon;
|
||||||
|
weather.region = data.city.country;
|
||||||
|
weather.city = data.city.name;
|
||||||
|
weather.description = data.list[0].weather[0].description;
|
||||||
|
|
||||||
location = location.join( "," );
|
location = location.join( "," );
|
||||||
|
|
||||||
@@ -196,7 +199,10 @@ exports.showWeatherData = function( req, res ) {
|
|||||||
location = [ parseFloat( location[ 0 ] ), parseFloat( location[ 1 ] ) ];
|
location = [ parseFloat( location[ 0 ] ), parseFloat( location[ 1 ] ) ];
|
||||||
|
|
||||||
// Continue with the weather request
|
// Continue with the weather request
|
||||||
getOWMWeatherData( location, function( data ) { res.json( data ); } );
|
getOWMWeatherData( location, function( data ) {
|
||||||
|
data.location = location;
|
||||||
|
res.json( data );
|
||||||
|
} );
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// Attempt to resolve provided location to GPS coordinates when it does not match
|
// Attempt to resolve provided location to GPS coordinates when it does not match
|
||||||
@@ -208,7 +214,10 @@ exports.showWeatherData = function( req, res ) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
location = result;
|
location = result;
|
||||||
getOWMWeatherData( location, function( data ) { res.json( data ); } );
|
getOWMWeatherData( location, function( data ) {
|
||||||
|
data.location = location;
|
||||||
|
res.json( data );
|
||||||
|
} );
|
||||||
} );
|
} );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
var express = require( "express" ),
|
var express = require( "express" ),
|
||||||
weather = require( "./routes/weather.js" ),
|
weather = require( "./routes/weather.js" ),
|
||||||
|
cors = require( "cors" ),
|
||||||
host = process.env.HOST || "127.0.0.1",
|
host = process.env.HOST || "127.0.0.1",
|
||||||
port = process.env.PORT || 3000,
|
port = process.env.PORT || 3000,
|
||||||
app = express();
|
app = express();
|
||||||
@@ -17,7 +18,7 @@ app.get( /weather(\d+)\.py/, weather.getWeather );
|
|||||||
app.get( /(\d+)/, weather.getWeather );
|
app.get( /(\d+)/, weather.getWeather );
|
||||||
|
|
||||||
// Handle requests matching /weatherData
|
// Handle requests matching /weatherData
|
||||||
app.get( /weatherData/, weather.showWeatherData );
|
app.get( /weatherData/, cors(), weather.showWeatherData );
|
||||||
|
|
||||||
app.get( "/", function( req, res ) {
|
app.get( "/", function( req, res ) {
|
||||||
res.send( "OpenSprinkler Weather Service" );
|
res.send( "OpenSprinkler Weather Service" );
|
||||||
|
|||||||
Reference in New Issue
Block a user