| | |
| | | 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) { |
| | |
| | | |
| | | 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); |
| | |
| | | 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') ? '℉' : '℃'; |
| | | res.render('index', {weather: weather, error: null, units: unit_hex}); |
| | | } |
| | | } |
| | | catch (err) { |