-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.js
36 lines (34 loc) · 1.06 KB
/
root.js
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
var router = require("express").Router();
const request = require("request");
require("dotenv").config();
const apiKey = process.env.WEATHER_API_KEY;
router.get("/", function (req, res) {
let city = "gurgaon";
let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`;
request(url, function (err, response, body) {
if (err) {
res.render("index", {
weather: null,
error: "Error, please try again",
AESKey: process.env.AES_KEY,
});
} else {
let weather = JSON.parse(body);
if (weather.main == undefined) {
res.render("index", {
weather: null,
error: "Error, please try again",
AESKey: process.env.AES_KEY,
});
} else {
let weatherText = `It's ${weather.main.temp} degrees Celcius with ${weather.weather[0].main} in ${weather.name}!`;
res.render("index", {
weather: weatherText,
error: null,
AESKey: process.env.AES_KEY,
});
}
}
});
});
module.exports = router;