-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
39 lines (34 loc) · 1.43 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
require('dotenv').config();
const express = require("express");
const https = require("https");
const bodyParser = require("body-parser");
const app = express();
const PORT = process.env.PORT || 3000;
app.use(bodyParser.urlencoded({extended: true}));
app.get("/", function(req, res) {
res.sendFile(__dirname+"/index.html");
});
app.post("/", function(req, res) {
console.log("Post request successful");
const query = req.body.cityName;
const appKey = process.env.APP_KEY;
const unit = "metric";
const url = "https://api.openweathermap.org/data/2.5/weather?q="+ query +"&appid="+ appKey +"&units=" + unit;
https.get(url, function(response) {
response.on('data', function(d) {
const weatherData = JSON.parse(d);
const temp = weatherData.main.temp;
const desc = weatherData.weather[0].description;
const icon = "https://openweathermap.org/img/wn/"+ weatherData.weather[0].icon + "@4x.png";
res.write("<body style='background-color: #9EB384';><div align='center'><br><br>")
res.write("<h1>The temperature in "+ query + " is "+ temp +" degree celsius.</h1>");
res.write("<h2>Weather description: "+ desc +".</h2>")
res.write("<img src="+icon+">")
res.write("</div></body>");
res.send();
});
})
});
app.listen(PORT, function() {
console.log(`Server is running on port ${PORT}`);
});