Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
84 lines (67 loc) · 2.65 KB
/
Copy pathapp.js
File metadata and controls
84 lines (67 loc) · 2.65 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
import express from 'express';
import https from 'https';
import secrets from './secrets.js';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import bodyParser from 'body-parser';
const app = express();
const __dirname = dirname(fileURLToPath(import.meta.url));
const apiKey = secrets.apiKey;
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static("public"));
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
app.get("/contact.html", function (req, res) {
res.sendFile(__dirname + "/contact.html");
});
app.get("/about.html", function (req, res) {
res.redirect("https://github.com/tsanevp/Weather-App");
});
app.post("/", function (req, res) {
const query = req.body.cityName;
let units = "imperial";
if (req.body.unitsCelcius) units = "metric";
const url = "https://api.openweathermap.org/data/2.5/weather?q=" + query + "&units=" + units + "&appid=" + apiKey;
https.get(url, (response) => {
response.on('data', (data) => {
const weatherData = JSON.parse(data);
if (weatherData.cod !== 200) {
const invalidLocation = query;
res.send({ invalidLocation });
return;
}
const imageId = weatherData.weather[0].icon;
const imageURL = "https://openweathermap.org/img/wn/" + imageId + "@2x.png";
const temp = weatherData.main.temp;
const description = weatherData.weather[0].description;
const feelsLike = weatherData.main.feels_like;
const humidity = weatherData.main.humidity;
const wind = weatherData.wind.speed;
const tempMax = weatherData.main.temp_max;
const tempMin = weatherData.main.temp_min;
res.send({ imageURL, temp, description, feelsLike, humidity, wind, tempMax, tempMin });
});
}).on('error', (e) => {
res.send(e);
});
});
app.post("/contact.html", function (req, res) {
const { name, emailAddress, message } = req.body;
const url = "https://docs.google.com/forms/d/e/1FAIpQLSddoLVzy0JmAmq2P5ynYhaX8hUBJ22c3yuNFlWgt7Z13SBJ3A/formResponse?entry.225115399=" + name + "&entry.90530336=" + emailAddress + "&entry.2053798052=" + message;
https.get(url, (response) => {
let complete = true;
if (response.statusCode !== 200) {
complete = false;
res.send({ complete });
return;
}
res.send({ complete });
}).on('error', (e) => {
res.send(e);
});
});
app.listen(8080, function () {
console.log("Server is running on port 3000.")
})