-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
262 lines (231 loc) · 8.42 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
const express = require("express");
const https = require("https");
const app = express();
app.use(express.static("public"));
const bodyParser = require("body-parser");
const { error } = require("console");
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", (req, res) => {
console.log("get method called");
res.sendFile(__dirname + "/index.html");
});
app.post("/", (req, res) => {
console.log("post method called");
const CitySearched = req.body.cityName;
const url_api =
"https://api.openweathermap.org/data/2.5/weather?q=" +
CitySearched +
"&appid=bbde3cc20f37d6c576ef76bf7ae7539b";
https.get(url_api, (response) => {
response.on("data", (data) => {
const weatherData = JSON.parse(data);
const temp = (weatherData.main.temp - 273.15).toFixed(2);
const weatherDescription = weatherData.weather[0].description;
const Humidity = weatherData.main.humidity;
const windSpeed = weatherData.wind.speed;
const countryName = weatherData.sys.country;
// var weatherImg= weatherData.weather[0].description;
// const bodyElement = document.getElementById("body");
// if(weatherImg.includes('clouds')){
// res.write( 'bodyElement.style.backgroundColor="linear-gradient(135deg, #808080 0%, #363636 100%)";');
// }else if(weatherImg.includes('rain')){
// weatherImg = 'rainy';
// res.write('bodyElement.style.backgroundColor = "linear-gradient(135deg, #808080 0%, #363636 100%)";')
// }
// else if(weatherImg.includes('clear')){
// res.write('bodyElement.style.backgroundColor = "linear-gradient(135deg, #FFA500 0%, #FF4500 100%);";')
// }
// else if(weatherImg.includes('snow')){
// res.write('bodyElement.style.backgroundColor = "linear-gradient(135deg, #808080 0%, #363636 100%)";')
// }
// Check weather City name exist or not
// if(url_api.statusCode >= 230){
// res.write('<h1>City not found</h1>');
// res.send();
// }
// Handle error or city not found case
res.write(
`
<head><meta charset="utf-8"><title>Weather App</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Headland+One&family=Josefin+Slab:ital,wght@1,600&family=Merriweather:ital,wght@0,300;0,400;0,700;1,300;1,400;1,700;1,900&family=Montserrat:ital,wght@0,300;0,400;0,500;0,600;1,300&family=Open+Sans:ital,wght@0,300;0,400;0,700;1,300;1,600;1,700&family=Sacramento&display=swap" rel="stylesheet">
<style>
*{
margin: 0;
padding: 0;
}
body {
font-family: 'Headland One', serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin 0;
background-image: url("../images/pexels-priscilla-serneo-1588846.jpg") ;
// background-image: url("../images/pexels-eberhard-grossgasteiger-691574.jpg") ;
/* background-image: url("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSOB91u3pHkrhkfwtVHNV0pc0F7enPRJVUcVkRvvcMQ&s" ); */
background-repeat: no-repeat;
background-size:cover;
}
/* to make header top of screen */
header{
position: absolute;
top: 0;
left: 0;
width: 100%;
padding: 0em;
text-align: center;
z-index: -1;
}
header>h1{
font-family: 'Headland One', serif;
font-size: 3.2em;
font-weight: 400;
color: #fff;
}
.card{
background-image: url("../images/pexels-amy-chandra-789152.jpg");
background-size:100%;
opacity: 0.8;
color: #000000 !important;
box-shadow
:
0
2px
4px
rgba
(
0
,
0
,
0
,
0.1
);
padding: 2em;
border: 1px solid white;
border-radius: 28px;
width: 100%;
max-width: 420px;
margin: 1em;
}
form
{
margin-bottom
:
20px
;
}
.search {
display: flex;
justify-content: center;
align-items: center;
margin: 0.5em;
}
h4{
padding: 0.5em;
}
button{
border: none;
margin: 0.3em;
/* padding: 0.1em; */
background:#7c7c7c2b;
color: #fff;
height: 2.6em;
width: 2.6em;
border-radius: 50%;
font-size: 1.2em;
cursor: pointer;
transition: 0.22s ease-in-out;
}
#country{
font-size: 1.2em;
font-weight: 700;
color: #fff;
// position: absolute;
padding: 0 0 0 0.5em;
}
button:hover{
background: #595757;
}
input.citySearched{
border: none;
outline: none;
border-radius: 11px;
padding: 0.45em;
color: #fff;
/* greyish */
background:#494444a9;
width: calc(100%-100px);
font-size: 120%;
}
.weather-result {
color: #fff;
}
.weather-description {
color: #fff;
}
.humidity {
color: #fff;
}
.windSpeed {
color: #fff;
}
`
);
res.write("</style>");
// res.write(
// '<style>*{margin:0;padding:0}body{font-family:\'Open Sans\',sans-serif;display:flex;justify-content:center;align-items:center;height:100vh;margin:0;background:#302f2f;position:relative;background-image:url(./images/pexels-johannes-plenio-1119974.jpg);background-size:1300px 630px;background-position:center;background-repeat:no-repeat}.card{background-color:#000000b0!important;color:#fff!important;padding:2em;border:1px solid white;border-radius:28px;width:100%;max-width:420px;margin:1em}.search{display:flex;justify-content:center;align-items:center;margin:.5em}button{border:none;margin:.3em;background:#7c7c7c2b;color:#fff;height:2.6em;width:2.6em;border-radius:50%;font-size:1.2em;cursor:pointer;transition:.22s ease-in-out}button:hover{background:#595757}input.citySearched{border:none;outline:none;border-radius:11px;padding:.45em;color:#fff;background:#494444a9;width:calc(100%-100px);font-size:120%}</style>'
// );
res.write("</head>");
res.write('<body id="body">');
res.write("<header><h1>Weather App</h1></header> ");
res.write('<div class="card">');
res.write('<form action="/" method="POST">');
res.write('<div class="search">');
// res.write('<img src="./images/" alt="search icon">')
res.write(
'<input type="text" name="cityName" placeholder="Weather for city" class="citySearched">'
);
res.write(
'<button type="submit" > <svg xmlns="http:https://www.w3.org/2000/svg" height="1em" viewBox="0 0 512 512">! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc.<style>svg{fill:#ffffff}</style><path d="M416 208c0 45.9-14.9 88.3-40 122.7L502.6 457.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0S416 93.1 416 208zM288 176c0-44.2-35.8-80-80-80s-80 35.8-80 80c0 48.8 46.5 111.6 68.6 138.6c6 7.3 16.8 7.3 22.7 0c22.1-27 68.6-89.8 68.6-138.6zm-112 0a32 32 0 1 1 64 0 32 32 0 1 1 -64 0z"/></svg></button>'
);
res.write("</div>");
res.write(' <div class="weather-at">');
res.write(
'<h2 h2 class="city"> Weather in ' +
CitySearched +
"<span id=country>" +
countryName +
"</span>" +
"</h2><div><h1 >" +
temp +
" °C </h1>"
);
res.write("</div>");
res.write('<div class="gap"> ');
// res.write('<i class="fa-regular fa-clouds"></i>');
res.write(
'<div class="weather-description description"><h4>The weather is currently ' +
weatherDescription +
"</h4></div>"
);
res.write(
'<div class="humidity"><h4>Humidity: ' + Humidity + "%</h4></div>"
);
res.write(
'<div class="windSpeed"><h4>Wind Speed: ' +
windSpeed +
"Km/Hour</h4></div>"
);
res.write("</form>");
res.write("</div></body>");
res.send();
});
});
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});