diff --git a/weather/routes/index.js b/weather/routes/index.js index a3cbcc4..e45d70f 100644 --- a/weather/routes/index.js +++ b/weather/routes/index.js @@ -10,30 +10,27 @@ router.get('/', function(req, res) { res.render('index', { weather: null, err: null }); }); -router.post('/get_weather', async function (req,res) { - let city = req.body.city; - let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=${UNITS}&appid=${OWM_API_KEY}`; +router.post('/get_weather', async function (req, res) { + let lat = req.body.lat; // Latitude from the form + let lon = req.body.lon; // Longitude from the form + let url = `http://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&units=${UNITS}&appid=${OWM_API_KEY}`; try { let data = await fetch(url); let weather = await data.json(); console.log(weather); - if(weather.cod == '404' && weather.main == undefined) { - res.render('index', {weather: null, error: 'Error: Unknown city'}); + if (weather.cod == '404' && weather.main == undefined) { + res.render('index', { weather: null, error: 'Error: Unknown location' }); + } else if (weather.cod == '401' && weather.main == undefined) { + res.render('index', { weather: null, error: 'Error: Invalid API Key. Please see http://openweathermap.org/faq#error401 for more info.' }); + } else { + let unit_hex = (UNITS == 'imperial') ? '℉' : '℃'; + res.render('index', { weather: weather, error: null, units: unit_hex }); } - else if (weather.cod == '401' && weather.main == undefined) { - res.render('index', {weather: null, error: 'Error: Invalid API Key. Please see http://openweathermap.org/faq#error401 for more info.'}); - } - else { - let unit_hex = (UNITS == 'imperial') ? '℉' : '℃'; - res.render('index', {weather: weather, error: null, units: unit_hex}); - } - } - catch (err) { + } catch (err) { console.log(err); - res.render('index', {weather: null, error: 'Error: Unable to invoke OpenWeatherMap API'}); + res.render('index', { weather: null, error: 'Error: Unable to invoke OpenWeatherMap API' }); } - }); module.exports = router; diff --git a/weather/views/index.pug b/weather/views/index.pug index 6ed604a..6ec21df 100644 --- a/weather/views/index.pug +++ b/weather/views/index.pug @@ -1,30 +1,33 @@ extends layout + block content .container - h2 - p - | OpenShift Weather App - br + h2 + p OpenShift Weather App + br + br + .row + .col-md-3 + form(method="post", action="get_weather") + .form-group + label.sr-only(for="lat") Latitude + input.form-control(name="lat" type="number" step="any" placeholder="Enter Latitude" required='required') + .form-group + label.sr-only(for="lon") Longitude + input.form-control(name="lon" type="number" step="any" placeholder="Enter Longitude" required='required') + button.btn.btn-success(type="submit") Get Weather + if error br + .alert.alert-danger #{error} + if weather .row - .col-md-3 - form(method="post", action="get_weather") - .form-group - label.sr-only(for="city") City Name - input.form-control( name="city" type="text" placeholder="Enter City Name (for ex: New York)" required='required') - button.btn.btn-success(type="submit") Get Weather - if error - br - .alert.alert-danger #{error} - if weather - .row - .col-md-5 - p - br - .panel.panel-primary - .panel-heading Weather for: #{weather.name}, #{weather.sys.country} - .panel-body - p Current Temperature = #{weather.main.temp} !{units}, #{weather.weather[0].description} - p Humidity = #{weather.main.humidity} % - p Min = #{weather.main.temp_min} !{units} - p Max = #{weather.main.temp_max} !{units} \ No newline at end of file + .col-md-5 + p + br + .panel.panel-primary + .panel-heading Weather for: #{weather.name}, #{weather.sys.country} + .panel-body + p Current Temperature = #{weather.main.temp} !{units}, #{weather.weather[0].description} + p Humidity = #{weather.main.humidity} % + p Min = #{weather.main.temp_min} !{units} + p Max = #{weather.main.temp_max} !{units}