-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (35 loc) · 1.26 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
52
53
54
55
56
57
58
const express = require("express");
const https = require("https");
const app = express();
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended: true}));
var description;
var temp;
var url;
var city;
var icon;
app.get('/', function(req,res){
res.sendFile(__dirname+"/index.html");
});
app.post('/', function(req,res){
city = req.body.cityName;
url = "https://api.openweathermap.org/data/2.5/weather?q="+city+"&units=metric&appid=APIKEY";
https.get(url, function(response){
response.on("data", function(data){
const weatherData = JSON.parse(data);
temp = weatherData.main.temp;
description = weatherData.weather[0].description
icon = weatherData.weather[0].icon
var iconUrl = "http://openweathermap.org/img/wn/"+icon+"@2x.png";
res.write("<h1>Weather Stats</h1>");
res.write("<img src="+iconUrl+">");
res.write("<br>The weather in "+"<h3>"+city+"</h3>"+" is "+description+" and the temperature is "+"<h3>"+temp+"</h3>");
res.send();
console.log(temp);
console.log(description);
});
});
});
app.listen(3000, function(){
console.log("server running on port 3000");
});