Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (83 loc) · 3.81 KB
/
Copy pathindex.js
File metadata and controls
98 lines (83 loc) · 3.81 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
98
require('dotenv').config();
const express = require('express');
const hbs = require('hbs');
const bodyParser = require('body-parser');
const geocode = require('./geocode/geocode');
const weather = require('./weather/weather');
const port = process.env.PORT || 3000;
let app = express();
app.set('view engine', 'hbs');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.use(express.static(__dirname + '/public'));
app.get('/', (req, res) => {
getDetails('St Joseph, MO, USA', res);
// res.render('home.hbs', {
// pageTitle: 'Weather App - Weather'
// });
});
app.post('/', (req, res) => {
console.log(req.body.inputAddress);
getDetails(req.body.inputAddress, res);
});
app.listen(port, () => {
console.log(`Server is up on port ${port}`);
});
let getDetails = (addr, res) => {
geocode.geocodeAddress(addr, (errorMsg, results) => {
if (errorMsg) {
console.log(errorMsg);
} else {
console.log(results.address);
console.log(results.latitude);
console.log(results.longitude);
weather.getWeather(results.address, results.latitude, results.longitude, (errorMsg, weatherResults) => {
if (errorMsg) {
console.log(errorMsg);
} else {
//console.log(`It's currently ${weatherResults.currentlyTemp}. It feels like ${weatherResults.currentlyApparentTemp}.`);
//console.log(`Low: ${weatherResults.dailyLow}`);
if (weatherResults.currentlyUvIndex <= 2) {
uVIndexSpanId = 'green';
} else if (weatherResults.currentlyUvIndex >= 3 & weatherResults.currentlyUvIndex <= 5) {
uVIndexSpanId = 'yellow';
} else if (weatherResults.currentlyUvIndex >= 6 & weatherResults.currentlyUvIndex <= 7) {
uVIndexSpanId = 'orange';
} else if (weatherResults.currentlyUvIndex >= 8 | weatherResults.currentlyUvIndex <= 10) {
uVIndexSpanId = 'red';
} else if (weatherResults.currentlyUvIndex >= 11) {
uVIndexSpanId = 'purple';
}
res.render('home.hbs', {
pageTitle: 'Weather App - Weather',
currentlyTemp: weatherResults.currentlyTemp,
currentlyApparentTemp:weatherResults.currentlyApparentTemp,
currentlySummary: weatherResults.currentlySummary,
currentlyWindSpeed: weatherResults.currentlyWindSpeed,
currentlyWindBearing: weatherResults.currentlyWindBearing,
currentlyHumidity: Math.round(weatherResults.currentlyHumidity*100),
currentlyDewPoint: weatherResults.currentlyDewPoint,
currentlyUvIndex: weatherResults.currentlyUvIndex,
uVIndexSpanId: uVIndexSpanId,
currentlyVisibility: Math.round(weatherResults.currentlyVisibility),
currentlyPressure: Math.round(weatherResults.currentlyPressure),
dailyLow: weatherResults.dailyLow,
dailyHigh: weatherResults.dailyHigh,
hourlySummary: weatherResults.hourlySummary,
iconImg: weatherResults.currentlyIcon,
location: results.address,
lat: results.latitude,
lng: results.longitude,
hours: weatherResults.hours,
temps:weatherResults.temps,
timelineColors: weatherResults.timelineColors,
dailySummary: weatherResults.dailySummary,
weeklyDetails: weatherResults.weeklyDetails
});
}
});
}
});
};