Ravi Srinivasan
2019-09-13 1ed32078a531c8ec7e059fafbf87c7f7bf57258d
Added logic for converting between metric and imperial units
2 files modified
14 ■■■■■ changed files
weather/routes/index.js 6 ●●●●● patch | view | raw | blame | history
weather/views/index.pug 8 ●●●● patch | view | raw | blame | history
weather/routes/index.js
@@ -3,6 +3,7 @@
const fetch = require("node-fetch");
require('dotenv').config();
const OWM_API_KEY = process.env.OWM_API_KEY || 'invalid_key';
const UNITS = process.env.UNITS || 'metric';
/* GET home page. */
router.get('/', function(req, res) {
@@ -11,7 +12,7 @@
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=metric&appid=${OWM_API_KEY}`;
  let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=${UNITS}&appid=${OWM_API_KEY}`;
  try {
    let data = await fetch(url);
@@ -24,7 +25,8 @@
      res.render('index', {weather: null, error: 'Error: Invalid API Key. Please see http://openweathermap.org/faq#error401 for more info.'});
    }
    else {
      res.render('index', {weather: weather, error: null});
      let unit_hex = (UNITS == 'imperial') ? '&#8457' : '&#8451';
      res.render('index', {weather: weather, error: null, units: unit_hex});
    }
  }
  catch (err) {
weather/views/index.pug
@@ -24,7 +24,7 @@
            .panel.panel-primary
              .panel-heading Weather for: #{weather.name}, #{weather.sys.country}
              .panel-body
               p Current Temperature => #{weather.main.temp} ℃, #{weather.weather[0].description}
               p Humidity => #{weather.main.humidity} %
               p Min => #{weather.main.temp_min} ℃
               p Max => #{weather.main.temp_max} ℃
               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}