-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (34 loc) · 1.39 KB
/
app.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const express = require('express');
const https = require("https");
const app = express()
const bodyParser = require("body-parser");
const ejs = require("ejs");
const port = process.env.PORT || 3000;
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
app.set("view engine", "ejs");
app.get("/", function(req, res) {
res.render("home");
});
app.post("/", function(req, res) {
const query = req.body.cityName;
const unit = "units=metric"
const apiKey = "7ee464c85f036e0568018df80b538645";
const url = "https://api.openweathermap.org/data/2.5/weather?q=" + query + "&" + unit + "&appid=" + apiKey + ""
https.get(url, function(response) {
console.log(response.statusCode);
response.on("data", function(data) {
console.log(req.body.cityName);
const weatherData = JSON.parse(data);
const temp = weatherData.main.temp;
const description = weatherData.weather[0].description;
const icon = weatherData.weather[0].icon;
const location = query.charAt(0).toUpperCase() + query.slice(1);
const imageURL = 'http://openweathermap.org/img/wn/' + icon + '@2x.png';
res.render("weather", {temp: temp, description: description, icon:icon, location: location, imageURL:imageURL});
})
})});
//Run server//
app.listen(process.env.PORT || 3000, function(){
console.log("Server running on port 3000");
});