Skip to content

Commit

Permalink
Added logic for converting between metric and imperial units
Browse files Browse the repository at this point in the history
  • Loading branch information
rsriniva committed Sep 13, 2019
1 parent 6b13d51 commit 1ed3207
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 4 additions & 2 deletions weather/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const router = express.Router();
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) {
Expand All @@ -11,7 +12,7 @@ router.get('/', function(req, res) {

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);
Expand All @@ -24,7 +25,8 @@ router.post('/get_weather', async function (req,res) {
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) {
Expand Down
8 changes: 4 additions & 4 deletions weather/views/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ block content
.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}

0 comments on commit 1ed3207

Please sign in to comment.