Skip to content

Instantly share code, notes, and snippets.

@zacck-zz
Created March 27, 2017 07:15
Show Gist options
  • Save zacck-zz/0bf454301af10533e256b40692f25698 to your computer and use it in GitHub Desktop.
Save zacck-zz/0bf454301af10533e256b40692f25698 to your computer and use it in GitHub Desktop.
var request = require('request');
module.exports = function (ctx, req, res) {
request({url: 'http://ipinfo.io/', json:true}, (error, response, body) => {
//do we have an error guessing location of user
if(error) {
//an error occured lets alert user
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(`<p style="text-align: center">Unable to guess your Location ${error} Occured</p>`);
} else {
// if we have a response body.city will have our city lets use this
// as a variable for open weather
const city = body.city
const key = 'b3c17071c6f0bf5ed04f42571244404a';
var baseURL = `http://api.openweathermap.org/data/2.5/weather?APPID=${key}`;
var cityURL = `${baseURL}&&q=${city}`;
request({url: cityURL, json: true}, (error, response, body) => {
if(error) {
//an error occured lets alert the user
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(`<p style="text-align: center">Our system broke due to ${error}</p>`);
} else {
// //yay we got the weather and location!
const weather = body.weather[0].main;
const high = body.main.temp_max;
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(`<p style="text-align: center">Hello! The weather in ${city} is ${weather} with highs of ${high}</p>`);
}
})
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment