Repository navigation
Expand file tree
/
Copy pathWeather.js
More file actions
96 lines (89 loc) · 2.68 KB
/
Copy pathWeather.js
File metadata and controls
96 lines (89 loc) · 2.68 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const express = require("express");
// const cors = require("cor");
const app = express();
const bodyParser = require("body-parser");
const https = require("https");
// const ejs = require("ejs")
const port = process.env.PORT || 5000
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + "/public"));
// app.use(cors(
// {
// origin: ["https://weather-app-phi-six-51.vercel.app/"],
// method:["POST","GET"],
// credentials:true
// }
// ));
app.get("/", (req, res) => {
const cityimageurl = "citypng.png";
const heading = "Weather App";
const inputcode = true;
res.render("template", {
cityimageurl: cityimageurl,
inputcode: inputcode,
heading: heading,
});
});
const apikey = "b5fb35dd4753e1ad51c8ed9793f64d42";
app.post("/", (req, res) => {
const cityname = req.body.cityName;
const url = `https://api.openweathermap.org/data/2.5/weather?q=${cityname}
&appid=${apikey}&units=metric`;
https.get(url, function (response) {
console.log("dusre https.get tk kam kar rha hai");
response.on("data", function (data) {
const allData = JSON.parse(data);
const heading = `Temperature in ${cityname} is:`;
const temperature = allData.main.temp;
const inputcode = false;
const iconcode = allData.weather[0].icon;
const weatherdescription = allData.weather[0].description;
const weatherImage =
"http://openweathermap.org/img/w/" + iconcode + ".png";
// var weatherImage;
// switch (iconcode) {
// case "01d":
// weatherImage = `${iconcode}.png`;
// break;
// case "02d":
// weatherImage = `${iconcode}.png`;
// break;
// case "03d":
// weatherImage = `${iconcode}.png`;
// break;
// case "04d":
// weatherImage = `${iconcode}.png`;
// break;
// case "09d":
// weatherImage = `${iconcode}.png`;
// break;
// case "10d":
// weatherImage = `${iconcode}.png`;
// break;
// case "11d":
// weatherImage = `${iconcode}.png`;
// break;
// case "13d":
// weatherImage = `${iconcode}.png`;
// break;
// case "50d":
// weatherImage = `${iconcode}.png`;
// break;
// default:
// break;
// }
console.log(allData);
// res.write();
// res.write();
res.render("template", {
cityimageurl: weatherImage,
heading: heading,
inputcode: inputcode,
temperature: temperature,
weatherdescription:weatherdescription,
});
});
});
});
app.listen(port);