-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
44 lines (36 loc) · 1.69 KB
/
script.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
const express = require('express');
const bodyParser = require('body-parser');
const https = require('https');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html'); // Serve your HTML file
});
app.post('/', (req, res) => {
const nameofCity = req.body.cityName;
const url = 'https://api.openweathermap.org/data/2.5/weather?q=' + nameofCity + '&appid=091407001ef812a094e745e66dde647b&units=metric';
https.get(url, (response) => {
console.log(response.statusCode);
response.on('data', (data) => {
const weatherInfo = JSON.parse(data);
const temperature = weatherInfo.main.temp;
const weatherDescription = weatherInfo.weather[0].description;
const feelsLike = weatherInfo.main.feels_like;
const icon = weatherInfo.weather[0].icon;
const htmlResponse = `
<h1>The current temperature in ${nameofCity} is ${temperature} degree Celsius</h1>
<h2>The weather condition is: ${weatherDescription}</h2>
<img src="http://openweathermap.org/img/wn/${icon}@2x.png" alt="Weather Icon">
<h2>It feels like ${feelsLike} degree Celsius here in ${nameofCity}</h2>
<h2>Visibility is: ${weatherInfo.visibility}</h2>
<h2>The humidity is: ${weatherInfo.main.humidity}%</h2>
<h2>The wind speed is: ${weatherInfo.wind.speed} KM/H</h2>
`;
res.setHeader('Content-Type', 'text/html');
res.send(htmlResponse);
});
});
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});