Skip to content

Instantly share code, notes, and snippets.

@prof3ssorSt3v3
Created August 3, 2017 18:52
Show Gist options
  • Save prof3ssorSt3v3/0cdc5b0f118e06b8c1c0e255c3db704a to your computer and use it in GitHub Desktop.
Save prof3ssorSt3v3/0cdc5b0f118e06b8c1c0e255c3db704a to your computer and use it in GitHub Desktop.
// DarkSky API
// CORS requests are NOT allowed by the DarkSky server
// Requests from other Servers are allowed
// So, we would make a pass thru file on a server.
// Browser sends the fetch( ) AJAX request to OUR server
// OUR server sends the AJAX request to DarkSky server
// DarkSky responds to OUR Server
// OUR Server sends the DarkSky reply back to Browser
// Browser AJAX call => Our Server => DarkSky => Our Server => Browser AJAX call
// To simulate, we will make the call with NodeJS (outside a browser)
// this way, we can avoid the CORS issue.
// It requires installing node-fetch from the terminal
// npm install node-fetch
// Then add a require statement at the top of our script
//
let fetch = require('node-fetch');
//let uri = 'http://jsonplaceholder.typicode.com/users';
let darksky = 'https://api.darksky.net/forecast/';
let key = 'bc02564c54e3ec592f5c5c04e01f7b6a';
let lat = 45.3483;
let lng = -75.7584;
let uri = darksky + key + '/' + lat +','+ lng;
console.log(uri);
uri = uri.concat('?units=ca&exclude=minutely,hourly&lang=ru');
// units - ca, si, us, uk
// exclude - minutely,hourly,daily,currently
// lang -
let options = {
method: 'GET',
mode: 'cors'
}
let req = new fetch.Request(uri, options);
fetch(req)
.then((response)=>{
if(response.ok){
return response.json();
}else{
throw new Error('Bad HTTP!')
}
})
.then( (j) =>{
console.log(j.currently.temperature, j.currently.summary);
console.log( j.daily.data[1] );
//console.log('JSON data provided');
})
.catch( (err) =>{
console.log('ERROR:', err.message);
});
@bartektenDev
Copy link

Super helpful for Solar Panel software I'm working on. Thanks! <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment