-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.js
More file actions
97 lines (70 loc) · 3.7 KB
/
Copy pathapp.js
File metadata and controls
97 lines (70 loc) · 3.7 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
85
86
87
88
89
90
91
92
93
94
95
96
97
const express = require("express");
const https = require("https");
const bodyParser = require("body-parser");
const favicon = require("serve-favicon");
const path = require("path");
require('dotenv').config()
const timeObject = require('./public/scripts/time');
const { timeEnd } = require("console");
console.log(timeObject);
/* ---------------------------- Basic Connections --------------------------- */
const app = express();
/* ----------------------------- Set View Engine ---------------------------- */
app.set('view engine', 'pug');
/* ---------------------------- Set Views Folder ---------------------------- */
app.set('views', path.join(__dirname, 'src', 'views'));
/* ---------------------------- Set Static Folder --------------------------- */
app.use(express.static(path.join(__dirname, 'public')));
/* ---------------------------- Setting up CSS/JS Files --------------------------- */
app.use('/css', express.static(path.join(__dirname, 'node_modules/bootstrap/dist/css')));
app.use('/js', express.static(path.join(__dirname, 'node_modules/bootstrap/dist/js')));
/* ---------------------------- Body Parser Setup --------------------------- */
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
/* ---------------------------- Basic Routes --------------------------- */
app.get("/", function (req, res,) {
res.render('index', { title: "Global_Weather | Home", time: timeObject.time, day:timeEnd.day, dayMethod:timeObject.dayMethod, today:timeObject.today });
});
app.get("/about", function (req, res,) {
res.render('about', { title: "Global_Weather | About" });
});
app.get("*", function (req, res) {
res.render('error', { title: "Global_Weather | Error" });
});
app.post("/", function (req, res) {
const query = req.body.cityName
const apiKey = process.env.apiKey;
const unit = "metric"
const url = "https://api.openweathermap.org/data/2.5/weather?q=" + query + "&callback" + "&appid=" + apiKey + "&units=" + unit;
https.get(url, function (response) {
console.log(response.statusCode);
if (response.statusCode === 200) {
response.on("data", function (data) {
const weatherData = JSON.parse(data)
const weatherDescription = {
temp: weatherData.main.temp,
feels_like: weatherData.main.feels_like,
humidity: weatherData.main.humidity,
pressure: weatherData.main.pressure,
wind: weatherData.wind.speed,
weatherDesc: weatherData.weather[0].description.toUpperCase(),
weatherMain: weatherData.weather[0].main.toUpperCase(),
icon: weatherData.weather[0].icon,
imageURL: "https://openweathermap.org/img/wn/" + weatherData.weather[0].icon + "@2x.png",
name: weatherData.name.toUpperCase(),
country: weatherData.sys.country,
}
res.render('index', { title: "Global_Weather | Result", temp: weatherDescription.temp, feels_like: weatherDescription.feels_like, humidity: weatherDescription.humidity, pressure: weatherDescription.pressure, wind: weatherDescription.wind, weatherDesc: weatherDescription.weatherDesc, weatherMain: weatherDescription.weatherMain, icon: weatherDescription.icon, imageURL: weatherDescription.imageURL, name: weatherDescription.name, country: weatherDescription.country, time: timeObject.time, day:timeEnd.day, dayMethod:timeObject.dayMethod, today:timeObject.today })
})
} else {
console.log("Error");
res.render('error')
}
})
});
/* ---------------------------- Server Listening --------------------------- */
app.listen(3000, function () {
console.log("Server is running on port 3000.");
})