-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (49 loc) · 1.74 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
const express = require("express");
const path = require("path");
const https = require("https");
const app = express();
const bodyParser = require("body-parser");
const ejs = require("ejs");
const PORT = process.env.PORT || 3000;
// app.use(express.static(path.join(__dirname, "public")));
//add static directyory
app.use(express.static(path.join(__dirname, "")));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
app.post("/", function (req, res) {
const query = req.body.cityName;
const apiKey = "19a43bf507930cd0bc219692c8414450";
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("StatusCode = " + response.statusCode);
response.on("data", function (data) {
const weatheData = JSON.parse(data);
const temp = weatheData.main.temp;
const weatherDescription = weatheData.weather[0].description;
const icon = weatheData.weather[0].icon;
const iconURL = "http://openweathermap.org/img/wn/" + icon + "@2x.png";
weatheData.weather[0].icon = iconURL;
// const city = weatheData.name;
// const country = weatheData.sys.country;
// const windSpeed = weatheData.wind.speed;
// const humidity = weatheData.main.humidity;
// const pressure = weatheData.main.pressure;
res.render("result", { weather: weatheData });
console.log("Temp = " + temp);
console.log("WeatherDescription = " + weatherDescription);
});
});
});
app.listen(PORT, function () {
console.log("Server is running on port 3000");
});