Ravi Srinivasan
2019-09-09 d0d40918be12b8c94c4e8e5bc0158b160e48c82e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const express = require('express');
const router = express.Router();
const request = require('request');
require('dotenv').config();
const OWM_API_KEY = process.env.OWM_API_KEY || 'invalid_key';
 
/* GET home page. */
router.get('/', function(req, res) {
  res.render('index', { weather: null, err: null });
});
 
router.post('/get_weather', 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}`;
 
  request(url, function (err, response, body) {
    if (err) {
      res.render('index', {weather: null, error: 'Error: Unable to invoke OpenWeatherMap API'});
    }
    else {
      let weather = JSON.parse(body);
      console.log('response from OpenWeatherMap API: ' + body);
      if(weather.cod == '404' && weather.main == undefined) {
        res.render('index', {weather: null, error: 'Error: Unknown city'});
      }
      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 {
        res.render('index', {weather: weather, error: null});
      }
    }
  });
});
 
module.exports = router;