-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
45 lines (41 loc) · 1.24 KB
/
Copy pathapp.js
File metadata and controls
45 lines (41 loc) · 1.24 KB
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
const express = require("express");
const https = require("https");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
app.post("/", function (req, res) {
const cityname = req.body.cityName;
const apikey = "8944d08dd59aa3e86d55b10cb4ddd67c";
const units = "metric";
const url =
"https://api.openweathermap.org/data/2.5/weather?q=" +
cityname +
"&appid=" +
apikey +
"&units=" +
units;
https.get(url, function (response) {
response.on("data", function (data) {
const weatherData = JSON.parse(data); //to convert a JSON string
const temp = weatherData.main.temp;
const weatherDescription = weatherData.weather[0].description;
const icon = weatherData.weather[0].icon;
res.write(
"<h1> The temerature of " +
cityname +
" is " +
temp +
" Degree Celcius</h1>"
);
res.write("<h1> Weather condition " + weatherDescription + "</h1>");
res.write(
'<br><img src= "http://openweathermap.org/img/wn/' + icon + '@2x.png"> '
);
res.send();
});
});
});
app.listen(5000);