-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
72 lines (67 loc) · 2.5 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
const express = require("express");
const https = require("https");
const app = express();
app.use(express.urlencoded());
app.post("/", (req, res) => {
const query = req.body.cityName;
const units = "metric";
const apikey = "62f26e04d8e08b535201a0a6abe06176";
const url =
"https://api.openweathermap.org/data/2.5/weather?q=" +
query +
"&appid=" +
apikey +
"&units=" +
units;
https
.get(url, (response) => {
if (response.statusCode === 200) {
response.on("data", (data) => {
const weatherData = JSON.parse(data);
const weatherDesc = weatherData.weather[0].description;
const temp = weatherData.main.temp;
const weatherIcon = weatherData.weather[0].icon;
const imgUrl = `http://openweathermap.org/img/wn/${weatherIcon}@2x.png`;
res.send(`
<div class="container">
<h1>Weather App</h1>
<form action="/" method="post">
<p>Enter the city name</p>
<input type="text" name="cityName" placeholder="e.g., London" required />
<button type="submit">Go</button>
</form>
<div class="result">
<p class="weather-info">The temperature in ${query} is ${temp}°C</p>
<p class="weather-info">The weather is described as ${weatherDesc}</p>
<img class="weather-icon" src="${imgUrl}" alt="Weather Icon">
</div>
</div>
<script>
changeBackground("${weatherDesc}");
</script>
`);
});
} else {
res.status(response.statusCode).send(`
<div class="container">
<h1>Weather App</h1>
<form action="/" method="post">
<p>Enter the city name</p>
<input type="text" name="cityName" placeholder="e.g., London" required />
<button type="submit">Go</button>
</form>
<div class="result">
<h3> City does not exist. Please try a valid city name.</h3>
</div>
</div>
`);
}
})
.on("error", (err) => {
console.error("Error fetching weather data:", err.message);
res.status(500).send("<h3>Internal Server Error</h3>");
});
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});