-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
46 lines (39 loc) · 1.47 KB
/
app.js
File metadata and controls
46 lines (39 loc) · 1.47 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
46
const express = require("express");
const https = require("https");
const bodyParser = require("body-parser");
const PORT = process.env.PORT || 3030;
const app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static("public"));
app.get("/", function(req, res) {
res.sendFile(__dirname + "/index.html")
});
app.post("/", function(req, res) {
const query = req.body.city;
const apiKey = "1a2cecf1ca58c549ddca68dc06980c82";
const unit = "metric";
const url = "https://api.openweathermap.org/data/2.5/weather?q=" + query + "&appid=" + apiKey + "&units=" + unit;
https.get(url, function(response) {
console.log(response.statusCode);
response.on("data", function(data) {
const weatherData = JSON.parse(data);
// console.log(weatherData);
//console.log(JSON.stringify(weatherData));
const temp = weatherData.main.temp;
//console.log(temp);
const weatherDescription = weatherData.weather[0].description;
//console.log(weatherDescription);
const icon = weatherData.weather[0].icon;
const imageurl = "http://openweathermap.org/img/wn/" + icon + "@2x.png";
res.write("<p>The weather is currently " + weatherDescription + "</p>");
res.write("<h1>The temperature in " + query + " is " + temp + "degree Celcius.</h1>");
res.write("<img src=" + imageurl + "></img?")
res.send();
})
});
});
app.listen(PORT, function() {
console.log("Servere is running on port 3000");
})